Jump to content

Time format


Azyrus

Recommended Posts

Hi everyone :)

 

I have a column in an sql database which stores time in minutes.

I'm trying to turn these minutes into this kind of format:

  1:00pm

12:30am

  6:15pm

 

I looked on W3C and there was the %r which seemed to format this way but I cant get it to work >.<

 

$time1=$f3/60;
$time2= date_format($f4, '%r');

 

Does anyone know how to achieve this? THANKS!!!

Link to comment
Share on other sites

Hi guys,

 

Thanks so much for your help :) I'll try my best to remember to use php.net from now on!

 

The format is perfect but I think I need to do something to the time value.

 

In the database seems to store time in minutes the value for 6am is an integer of 360, 7am would be 420 and so on... (This is my understanding which could be wrong).

 

Using the code below:

<?php echo date("g:ia","$f4"); ?>

 

The output for 360 which is 6am = 1:06am.

 

Does anyone know how to do this? Thanks a lot! I really appreciate your help guys :)

 

Link to comment
Share on other sites

Use simple math functions. The % (modulus) returns the remainder of a division operation.

 

<?php

$time = 799;

$hour = floor($time/60);
$min = str_pad($time % 60,2,0,STR_PAD_LEFT);

echo $hour.':'.$min;
echo '<br><br>';

// For AM/PM
$half = 'AM';
if( $hour > 12 ) {
$half = 'PM';
$hour -= 12;
}

echo $hour.':'.$min.' '.$half;

?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.