PHP Time Difference Script - How?

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
thenext88
Forum Newbie
Posts: 1
Joined: Sat Jan 06, 2007 11:15 pm

PHP Time Difference Script - How?

Post by thenext88 »

So I really don't know how to make a "time difference" script, and I sort of need one. I'm also still new with php, so if I did some stupid things like put variables where they shouldn't be, please let me know as well.

I need a script that can take a date given in m d Y g:i format, and find out the difference between the current, and that date.

I tried to make a test script, which failed, because I keep getting 0 as my answer.

Any suggestions and help would be greatly appreciated.

Code: Select all

<?php 

$time = date('m d Y g:i');
echo "$time";

echo "<br><br>";

$tomorrow = mktime(date("g"),date("i"),0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("m d Y g:i", $tomorrow);

echo "<br><br>";

$start = strtotime ($time);

$end = strtotime ($tomorrow);

$diff = $end - $start;

echo "$diff";

?>
Edit: got rid of gets
Last edited by thenext88 on Sat Jan 06, 2007 11:39 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you accepting user input into this script? $_GET is looking for user input (via the URL.) I'm going to guess that's not your intention; so remove the $_GET business. You only need $time and $tomorrow.

$tomorrow doesn't need to be passed through strtotime(). And $time could easily parse wrong, so just use time().
Post Reply