PDA

View Full Version : this code


Vladdrac
01-21-2004, 02:17 PM
What is wrong with this code? Around 1000p till 1159p it goes into a negative number


<?php

$daycl = date("w");
$time = date("D, M j Y");
$timeoff = (date("G")-5);
$timel = date(":i:s");
$read = "The current Date is";
$kill = array( "What happened?",
"Case of the Mondays?",
"Is it Mardi Gras yet?",
"Hump Day",
"You are almost there",
"Let the blackout begin",
"Wish I had a video camera");

print("<span class='np' style='textalign: center;'>
$read:</span><br /> <b>$time</b><br />");

print("<i>$timeoff$timel</i><br />");

print("<span class='tbox' style='textalign: center;'>$kill[$daycl]</span>");

?>

Viper007Bond
01-21-2004, 03:37 PM
Use mktime() (http://www.php.net/manual/en/function.mktime.php) to perform calculations of time.

For example, this is how to make today at 1:53:23 PM a Unix timestamp:

$unixtimestamp = mktime(13,53,23,1,21,2004);That will get you 1074711203, the seconds since 00:00:00 on January 1st, 1970.

The great thing about mktime() is that you can perform operations on it and date() supports putting in an timestamp:

date("some format",$unixtimestamp);

So, either convert your times to Unix timestamps and work with them (like subtract 3600 for an hour, subtract another timestamp to get a seconds difference, etc.) or operate with mktime().

(Use time() to get current Unix timestamp by the way.)

Anyway, to performing time operations. To subtract say 15 hours the time we used above, just subtract 15 in the hours area. But wiat you say! That'd be 13-15 = -2 o'clock! Well, mktime() is smart and figures out the correct time (10 PM yesterday).

Those should help you do what you need to do.

Vladdrac
01-21-2004, 04:23 PM
great man, thanks alot!

y6y6y6
01-21-2004, 04:55 PM
Also keep in mind that date("G"); will return the hours for the local time *on the server*. So while it might be 10 pm where you are, date("G") might still return 1 if it's 1 am where the server is.

Vladdrac
01-21-2004, 04:59 PM
yes, I tried to account for it earlier by

$timeoff = (date("G")-5);

it seemed to work, but i think it was also returning that negative time.

I think the mktime() function should fix it, ill look into it later

Viper007Bond
01-22-2004, 03:25 AM
Indeed it will. mktime with account for negative times. :)