Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: change time format 15:30:00 to 3:30PM

Tags:

date

php

time

I have a little issue with php and the time format. I tried to change the time using the date() function.

$time = date('g:i a',$time);

But the result I get for $time=15:30:30 is 3:30 am

The Ante meridiem stay stuck on AM event if it's the afternoon.

Thanks.

like image 278
user1149497 Avatar asked Feb 02 '12 13:02

user1149497


2 Answers

Try something like this:

 $time = date("g:i a", strtotime("15:30:00"));
like image 76
SERPRO Avatar answered Oct 23 '22 15:10

SERPRO


If taken from database My past codes were

// get the Shift times
$shiftArray=array();
$num=0;


 $getShiftTimes=mysqli_query($link,"SELECT * FROM shift");

 while ($returnShiftTimes = mysqli_fetch_array($getShiftTimes)){
      $shiftArray[$num][0]=$returnShiftTimes['shift_id'];
      $shiftArray[$num][1]=$returnShiftTimes['start_time'];
      $shiftArray[$num][2]=$returnShiftTimes['end_time'];
      $num++;
 }

 $time = date_format(date_create($shiftArray[0][1]), 'g:i A');

From 14:00:00 will return 2:00 PM

like image 27
reddy Avatar answered Oct 23 '22 15:10

reddy