php - Check if date has past other date don't work in certain situations
I have done research on how to figure out how I can use a if else statement to determine if a certain date has past another date or not but I notice this script works how it should work but if the day is a certain number for example 2 in the $present_time variable then it gives out the right output which is the else statement
This works
INPUT
<?php
$time_format = 'n-j-Y g:i A';
$time_zone = 'America/phoenix';
$date = DateTime::createFromFormat($time_format, '9-2-2021 11:28 AM', new DateTimeZone($time_zone));
$present_time = $date->format($time_format);
$date = DateTime::createFromFormat($time_format, '9-21-2021 9:57 PM', new DateTimeZone($time_zone));
$end_time = $date->format($time_format);
if($present_time > $end_time) {
echo 'Present time has pass the end time.';
}
else{
echo 'Present time has not pass the end time.';
}
?>
OUTPUT
Present time has not pass the end time.
but if I change the $present_time variables day to a 3 then I get an unexpected output which is the if statement and I should not get the else statement to show because the $end_time obviously has a later date.
INPUT
<?php
$time_format = 'n-j-Y g:i A';
$time_zone = 'America/phoenix';
$date = DateTime::createFromFormat($time_format, '9-3-2021 11:28 AM', new DateTimeZone($time_zone));
$present_time = $date->format($time_format);
$date = DateTime::createFromFormat($time_format, '9-21-2021 9:57 PM', new DateTimeZone($time_zone));
$end_time = $date->format($time_format);
if($present_time > $end_time) {
echo 'Present time has pass the end time.';
}
else{
echo 'Present time has not pass the end time.';
}
?>
OUTPUT
Present time has pass the end time.
So how can I get this to work properly to show the correct condition properly in these situations.
I got this method based on this tutorial
https://write.corbpie.com/php-check-if-current-date-time-has-passed-a-set-date-time/
but I need to use the hyphen character so that is why it looks a little different from that tutorial.
Answer
Solution:
The issue is that you are formatting your dates and then comparing the strings when you should be comparing the DateTime objects themselves.
When you compare the strings "{{-code-3}code{-code-3}1}{-code-3}3{-code-3}..." to "{{-code-3}code{-code-3}1}{-code-3}21{-code-3}...", the first one will be bigger because it compares the string character by character.
- position 1
{{-code-3}code{-code-3}1}
&{{-code-3}code{-code-3}1}
are equal - position 2
{-code-3}
&{-code-3}
are equal - position 3
3
&2
are not equal.
<?php
$time_format = 'n{-code-3}j{-code-3}Y g:i A';
$time_zone = 'America/phoenix';
$first = DateTime::createFromFormat($time_format, '{{-code-3}code{-code-3}1}{-code-3}3{-code-3}2021 11:28 AM', new DateTimeZone($time_zone));
// $present_time = $first{-code-3}>format($time_format);
// Once formatted, these are strings. Don't compare these.
// string(17) "{{-code-3}code{-code-3}1}{-code-3}3{-code-3}2021 11:28 AM"
$second = DateTime::createFromFormat($time_format, '{{-code-3}code{-code-3}1}{-code-3}21{-code-3}2021 {{-code-3}code{-code-3}1}:57 PM', new DateTimeZone($time_zone));
// $end_time = $second{-code-3}>format($time_format);
// Once formatted, these are strings. Don't compare these.
// string(17) "{{-code-3}code{-code-3}1}{-code-3}21{-code-3}2021 {{-code-3}code{-code-3}1}:57 PM"
if($first > $second) {
echo 'Present time has pass the end time.';
} else {
echo 'Present time has not pass the end time.';
}
Source