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.
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.
{{-code-3}code{-code-3}1}
&{{-code-3}code{-code-3}1}
are equal{-code-3}
&{-code-3}
are equal3
&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.';
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.