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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.