Page 1 of 1

[SOLVED] Time conversions

Posted: Wed Oct 20, 2004 2:34 pm
by josh
The following code is used to convert times from a database into timestamps... it works for everything except for one of my items it is reading out the wrong AM/PM value, this is the only error i have

Code: Select all

<?php
$atime='12:30:00'; // Set the time
$arrampm='PM'; // Set wether it is am or pm

$atime = "$atime:$arrampm"; // put the time and am/pm into one string
list($h, $m, $s, $a) = split(':', $atime); // get the hours minutes and seconds and ampm into their own strings
if ($a=="PM") { $h += 12; } // if it is in the pm add 12 hours to it to make 24 hour format time
$flightt = mktime($h, $m, $s, date("m"), date("d"), date("Y")); // make the timestamp


/// Then i echo out the readable format of the timestamp for debugging purposes, in this case it says AM instead of PM
echo date ("h:i:s A", $flightt);
?>
?>

Posted: Wed Oct 20, 2004 3:02 pm
by Christopher
To convert to 24 hour I think you need to do something like:

Code: Select all

if ($h == 12) {
    $h = 0;
}
if ($a=="PM") {
    $h += 12;
}

Posted: Wed Oct 20, 2004 4:43 pm
by kettle_drum
You set the am/pm variable to PM at the start of the code and then you compair it to this value - so it will always be PM. You need to check the hour to see if its pm.

Code: Select all

if($h>=12){
   //its pm
}else{
   //its am
}
Where do you get the $atime from? If you create it yourself, then why not make things easier for you and store it as 24 hour or unix epoch to begin with.

Posted: Wed Oct 20, 2004 4:59 pm
by josh
ok thanks, that works i just thought i was doing something else wrong