Jump to content

Date time not working...


Badchip
 Share

Recommended Posts

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 by Badchip
Link to comment
Share on other sites

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

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

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 by Badchip
Link to comment
Share on other sites

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 by Badchip
Link to comment
Share on other sites

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

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 by Badchip
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...