i'm having issue with calculating time in php. It's a simple script: when a time is given (datenow), a function ordertime evaluates:
- if it is < 10AM --> it displays today at 12:30
- if it is < 14PM --> it displays we can't take the order
- and if it's none of above --> it displays tomorrow at 12:30
I have php 4.4.9 on my server, date_default_timezone_set doesn't work, so i have to use gmdate and calculate the offset. When i put the whole script, it output the day before (it's using the servertime which is not my current timezone). This is the code. Thanks for anyhelp.
<?php
$gmt = -3;
$datenow = gmdate("d-m-Y H:i:s", mktime(date("H")+ $gmt, date("i"), date("s"), date("m"), date("d"), date("Y")));
$date = strtotime($datenow);
function ordertime ($when)
{
global $date ;
$datearray = strtotime($when);
if ($datearray['hours'] < 10) {
// order ships today
return strtotime("today 12:30");
}
elseif ($datearray['hours'] < 14) {
// we can't take this order
echo "we can't take this order";
} else {
// order ships tomorrow
return strtotime("tomorrow 12:30");
}
}
// date_default_timezone_set("America/Argentina/Buenos_Aires"); // this does not work on php4.4.9
$delivery = ordertime($date);
if ($delivery) {
$deliverytime = strftime("%d-%m-%Y %H:%M:%S", $delivery);
echo $datenow."<br />";
echo $deliverytime;
}?>
Which gives me this output
OUTPUT
25-02-2012 03:46:22
24-02-2030 12:30:00
I've have done all the correction Kolink suggested. I've removed the second parameter in return strtotime
because it gives me an output of 05-08-2030 12:30:00
. And if i remove getdate, i have the same output in php5.
Notice: A non well formed numeric value encountered in D:\Docs\Web\USB Webserver\root\lunchbox\testserver\index.php on line 11
25-02-2012 03:25:21
31-12-1969 12:30:00 – chenci Feb 25 '12 at 06:31