0

i'm having issue with calculating time in php. It's a simple script: when a time is given (datenow), a function ordertime evaluates:

  1. if it is < 10AM --> it displays today at 12:30
  2. if it is < 14PM --> it displays we can't take the order
  3. 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.

Charles
  • 50,943
  • 13
  • 104
  • 142
chenci
  • 113
  • 1
  • 10

1 Answers1

0

Try passing $datenow as a second parameter to strtotime(). This should make the "today" or "tomorrow" relative to the correct time, and fix your problem.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • It will fix the problem but the ordertime function would be useless. The whole point is to use the function – chenci Feb 25 '12 at 06:08
  • I mean in the `return` values in your `ordertime` function. That's where the problem is stemming from. – Niet the Dark Absol Feb 25 '12 at 06:11
  • It outputs this 31-12-1969 12:30:00, very weird – chenci Feb 25 '12 at 06:20
  • Right-click the page and choose View Source. Find the part of the code where that date stuff is, and see what, exactly, PHP is outputting. Also, try adding `global $datenow` to the start of the `ordertime` function, since it may not be getting that variable in the scope. – Niet the Dark Absol Feb 25 '12 at 06:21
  • I already edited the original post, and it gives me an error, and view source says exacly the output
    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
  • What is `getdate`? Just `strtotime` should be enough. – Niet the Dark Absol Feb 25 '12 at 06:37
  • Hey did you see that error, it seems that `strtotime` can't acept the second parameter. Why? I removed getdate (and the second parameter of strtotime), it didn't output the Notice or 1969, but it's still 24-02-2012 12:30:00 – chenci Feb 25 '12 at 06:40
  • It looks like `$datenow` is a string representation of the date? In that case you need to pass it through `strtotime` on its own too. – Niet the Dark Absol Feb 25 '12 at 06:43
  • god damnit, i hate php4. All the problems happen to php4. And ican't change it because all my client's the old script only work on php4. Look the new output i edited above: 05-08-2030 12:30:00 [the same script you gave me work perfecly on php5 (my windows test server), but screw up in godaddy php4] – chenci Feb 25 '12 at 06:49
  • If you take the second parameter of `return strtotime` it output the day before – chenci Feb 25 '12 at 06:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8218/discussion-between-chenci-and-kolink) – chenci Feb 25 '12 at 17:05