Date calculation format

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
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Date calculation format

Post by evolozik »

hey guys
i need to calculate the time left, here is my code

Code: Select all

$end=$new['ending_date'];
$current=date('Y-m-d His');
$timeleft=$end-$current;
the $end is in the same format as $current
why don't i have a correct answer in the same format for $timeleft
Last edited by evolozik on Thu Jan 04, 2007 3:42 pm, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

what it looks like you'll get is the number of seconds left - is that what you want?
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

in fact i want the time left to be in the form
day-hour-mins-seconds
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

well.. convert it!

60 seconds in a minute, 60 minutes in an hour....
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

i can't use maths because

Code: Select all

$timeleft=$end-$current;
gives me zero
does php allow this kind of calculation?
till now in tutorials i saw calculation either with time being initialised by code and not from database or directly from 2 colums in the database
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I thought you were using time() for a second... you'll need to use strtotime() on each variable to get the unix timestamp, then subtract those:

Code: Select all

$timeleft=strtotime($end)-strtotime($current);
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

Thx Kieran Huggins, was able to solve it :D
Post Reply