[SOLVED] Time conversions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

[SOLVED] Time conversions

Post 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);
?>
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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;
}
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

ok thanks, that works i just thought i was doing something else wrong
Post Reply