Jump to content

Need help with a "time difference" script


thenext88

Recommended Posts

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]<?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";

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/33177-need-help-with-a-time-difference-script/
Share on other sites

[quote author=jesirose link=topic=121338.msg498777#msg498777 date=1168148006]
Take the $_GETs out...you're not getting anything from the url. You already have your variables.

$start = strtotime ($time);
$end = strtotime ($tomorrow);
$diff = $end - $start;
[/quote]

Ok, modified, and testing.

Edit: I still get 0 as an answer.
The entire output was:

01 07 2007 12:40

Tomorrow is 01 08 2007 12:40


0


The reason I have one timestampe and the other not as current time is because I have a MySQL database that contains a date of entry. And this date of entry, is what has to be subracted from the current time, which is why I am trying to get this work.

And when I removed strtotime from $tomorrow, I got:

01 07 2007 12:40

Tomorrow is 01 08 2007 12:40


1168278000

[quote author=jesirose link=topic=121338.msg498784#msg498784 date=1168148727]
[quote author=jesirose link=topic=121338.msg498780#msg498780 date=1168148338]
$tomorrow is already a timestamp, so you don't need to run strtotime on it again.
[/quote]

Take out the strtotime call on $tomorrow and try again. $end = $tomorrow.
[/quote]

I did that, and the answer is now 1168278000 ???

And yes, I set $end = $tomorrow

code is now

[code]<?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><br>";

$start = strtotime ("$time");
$end = $tomorrow;
$diff = $end - $start;

echo "$diff";

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.