I have date in string. i.e: 18/10/2018
and also date in an object: $pnrProduct['Date'] . i.e 20/10/2018
I would like to check if the second date is bigger then the first one (in this case it is bigger)
how can i do that?
The function strtotime()
is what you need. It takes a dateable string and converts it into seconds elapsed from epoch.
In simple terms:
if (strtotime(str_replace("/", "-", "18/10/2018")) > strtotime(str_replace("/", "-", "20/10/2018")))
echo "Greater";
else
echo "Smaller";
Just a heads up:
strtotime("18-10-2018")
int(1539835200)
strtotime("20-10-2018")
int(1540008000)
You might need to replace the separator as:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
you must use strtotime for parse a string to date
$firstDate = strtotime(str_replace("/", "-", "20/10/2018"));
$secondDate = strtotime(str_replace("/", "-", $pnrProduct['Date']));
if ($firstDate < $secondDate ) { /* do Something */ }
How about using simplyDateTime
class?
<?php
$datetime1 = DateTime::createFromFormat('d/m/Y','18/10/2018');
$datetime2 = DateTime::createFromFormat('d/m/Y','20/10/2018');
if($datetime1 > $datetime2){
echo 'datetime1 greater than datetime2';
} else {
echo 'datetime2 greater than datetime1';
}
?>
DEMO: https://3v4l.org/5hYBP