Badchip Posted November 20, 2017 Share Posted November 20, 2017 (edited) I'm trying to set a PHP in order to select another PHP between sunday and monday: date_default_timezone_set('Europe/Madrid'); $dtA = new DateTime('sunday 9:55PM'); $dtB = new DateTime('monday 1:00AM'); if ($dtA > $dtB) { header("Location: url1.php"); } else { header("Location: url2.php"); } It always send me to the url1.php. What am I doing wrong? Thank you in advance. Edited November 20, 2017 by Badchip Link to comment Share on other sites More sharing options...
Ingolme Posted November 20, 2017 Share Posted November 20, 2017 The DateTime objects can't be compared with the usual comparison operators because they're not primitive data types. Compare their timestamps instead: $dtA->getTimestamp() > $dtB->getTimestamp() Link to comment Share on other sites More sharing options...
Badchip Posted November 20, 2017 Author Share Posted November 20, 2017 How do I apply it? I have tried two ways but I get an http error 500: $dtA->getTimestamp('sunday 9:55PM') > $dtB->getTimestamp('monday 1:00AM'); if ($dtA > $dtB) { header("Location: url1.php"); } else { header("Location: url2.php"); } if ($dtA->getTimestamp('sunday 9:55PM') > $dtB->getTimestamp('monday 1:00AM')) { header("Location: url1.php"); } else { header("Location: url2.php"); } Thank you for your patience. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2017 Share Posted November 20, 2017 You don't need to change the first 3 lines of your original code. You only need to change the if statement. The getTimestamp method does not accept any parameters. http://php.net/manual/en/datetime.gettimestamp.php Link to comment Share on other sites More sharing options...
Badchip Posted November 20, 2017 Author Share Posted November 20, 2017 (edited) Something like this? (stills opening only the url1.php) date_default_timezone_set('Europe/Madrid'); $dtA = new DateTime('sunday 9:55PM'); $dtB = new DateTime('monday 1:00AM'); if ($dtA->getTimestamp() > $dtB->getTimestamp()) { header("Location: url1.php"); } else { header("Location: url2.php"); } Edited November 20, 2017 by Badchip Link to comment Share on other sites More sharing options...
Ingolme Posted November 20, 2017 Share Posted November 20, 2017 I would guess that Sunday is six days after Monday. According to the manual: http://php.net/manual/en/datetime.formats.relative.php Weeks always start on Monday. A weekday name moves to the next day of the given name. Link to comment Share on other sites More sharing options...
Badchip Posted November 21, 2017 Author Share Posted November 21, 2017 (edited) It works within the same day (for example, Sunday to Sunday)... but it does not work with different days (Sunday 9:55PM to Monday 1:30AM). Quote date_default_timezone_set('Europe/Madrid'); $current = new DateTime(); $start = DateTime::createFromFormat('D H:i', 'Sun 9:55PM'); $end = DateTime::createFromFormat('D H:i', 'Mon 1:30AM'); if (($current > $start) && ($current < $end)) { header("Location: url1.php"); } else { header("Location: url2.php"); } Edited November 21, 2017 by Badchip Link to comment Share on other sites More sharing options...
Ingolme Posted November 21, 2017 Share Posted November 21, 2017 You're trying to compare objects again. These objects cannot be compared using < or >. Get the timestamp from them using ->getTimestamp() and compare those. I still suspect that the dates you're getting are Monday today and Sunday next week, but you can find that out very easily by printing their values: echo 'Start: ' . $start->format('M d') . "\n<br>"; echo 'End: ' . $end->format('M d') . "\n<br>"; Link to comment Share on other sites More sharing options...
Badchip Posted November 21, 2017 Author Share Posted November 21, 2017 (edited) Can you give me a hand with the code? I need to open a stream between sunday 9:55PM and monday 1:30AM. I have solved the problem with 2 PHP's but I think it's not the best way. Quote PHP1: date_default_timezone_set('Europe/Madrid'); $current = new DateTime(); $start = DateTime::createFromFormat('D H:i', 'Sun 9:55PM'); $end = DateTime::createFromFormat('D H:i:s', 'Sun 11:59:59PM'); if (($current > $start) && ($current < $end)) { header("Location: event.php"); } else { header("Location: php2.php"); } Quote PHP2: date_default_timezone_set('Europe/Madrid'); $current = new DateTime(); $start = DateTime::createFromFormat('D H:i', 'Mon 00:00AM'); $end = DateTime::createFromFormat('D H:i', 'Mon 01:30AM'); if (($current > $start) && ($current < $end)) { header("Location: event.php"); } else { header("Location: no_event.php"); } Edited November 21, 2017 by Badchip Link to comment Share on other sites More sharing options...
justsomeguy Posted November 22, 2017 Share Posted November 22, 2017 You have to realize that when you tell it Sunday, it's going to get the next Sunday. If you run that at Monday at 1am, and tell it you want Sunday, it's going to get the next Sunday, not yesterday. You may need more logic to figure out what you want. For example, get the current day of the week first and go from there. If the current day is Sunday, and it's after 9:55, or the current day is Monday and it's before 1:30, then do whatever you want to happen. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now