Converting Time

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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Converting Time

Post by JPlush76 »

hey all,
lets say I have a time: 3:31 PM
just like that...

how do I convert that into a usable time so I can do time differences on?

for example I have IN TIME: 3:31 PM OUT TIME: 12:54 AM

I'd want to convert those into usable time stamps so I can say the difference between your in and out time is XX hours XX minutes

anyone do anything like that before?
thanks :)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

i once tried, forgot what i did but it didn't work lol....is that any help? :lol: LOL

but yea...i'd like to know too please :)
Last edited by qads on Wed Sep 03, 2003 6:01 pm, edited 1 time in total.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

thanks Qads! lol
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

that was quick lol
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

strtotime might help.
But without a date

Code: Select all

<?php
$sIn = '3:31 PM';
$sOut = '12:54 AM';

$nIn = strtotime($sIn);
$nOut = strtotime($sOut);

echo date('F j, Y, g:i a', $nIn), "\n", date('F j, Y, g:i a', $nOut);
?>
it will get confused. On the other hand

Code: Select all

<?php
$sIn = '9/4/2003 3:31 PM';
$sOut = '9/3/2003 12:54 AM';

$nIn = strtotime($sIn);
$nOut = strtotime($sOut);

echo date('F j, Y, g:i a', $nIn), "\n", date('F j, Y, g:i a', $nOut);
?>
works.
$nIn and $nOut are number of seconds since the unix epoch representing the two datetimes, so you might subtract $nIn from $nOut.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

hehe
Perhaps the following can give some more ideas?

Code: Select all

<pre>
<?php 
$in = '3:31 PM';
$out = '12:54 AM';
echo strtotime($in)."\n";
echo strtotime($out)."\n";
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Gah! :wink:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It's special mod that posts a previously stored reply the moment someone else presses the "post reply" button :twisted:
just kidding
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Hahaha
You have a new message!
Do you want to overwrite the users responce with your own? Select from:

a) As is...
b) Rewrite slightly...
c) You own code, in your own words...
Post Reply