Comparing two date in php [duplicate] - php

This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
php date compare [duplicate]
(2 answers)
Closed 9 years ago.
I need to compare two dates
d1= 12-11-01(YY-MM-DD) and d2=2008-05-02(YYYY-MM-DD)
comparison d2 <= d1
It is not working as per requirement
Here please note that I need to compare dates with different date formats.

Try with:
$d1 = strtotime('12-11-01');
$d2 = strtotime('2008-05-02');
if ( $d2 <= $d1 ) {}

DateTime is better than strtotime() because it can account for timezones and daylight savings time
$dt1 = new DateTime('12-11-01');
$dt2 = new DateTime('2008-05-02');
if ( $dt2 <= $dt1 ) {
}
See it in action

Do this :
if(strtotime('12-11-01') <= strtotime('2008-05-02'))
{
// do your task here
}

Try this
if(strtotime('12-11-01') <= strtotime('2008-05-02'))
{ }

Like below,
if(strtotime($date1) < strtotime($date2)) {
// do something
}
Research it further here
http://php.net/manual/en/function.strtotime.php

You can use this code:
$d1 = strtotime($date1);
$d2 = strtotime($date2);
if($d2 <= $d1){
// $date2 is before $date1
}

Related

PHP, why does php recognize May 1st is less than 28th April? [duplicate]

This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 1 year ago.
I am trying to compare previous date with today which is May 1st, but turns out that previous date is higher than today. I wonder why is this happened and how to solve it?
PHP
$api_date = '2021-04-28T06:30:00Z';
$previous_date = date('d-m-Y , H:i', strtotime($api_date));
$today_date = date('d-m-Y , H:i');
if($previous_date <= $today_date)
print_r('true');
else{
print_r('false'); //will return this, why?
}
solution: just change the date format to year-month-day, thx to #Nigel Ren
$api_date = '2021-04-28T06:30:00Z';
$previous_date = date('Y-m-d , H:i', strtotime($api_date));
$today_date = date('Y-m-d , H:i');
if($previous_date <= $today_date)
print_r('true'); //will return this
else{
print_r('false');
}

compare date string in php with mm-dd-yy format [duplicate]

This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 5 years ago.
i need to compare two date from string,
my dates:
first date: 11-11-19
second date: 11-24-17
so i try to
$firstdate = "11-11-19";
$seconddate = "11-24-17";
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
if i change < or > the if statement should change, but i get always the firsdate...
how to do to compare two dates in this forma mm-dd-yy ?
Thanks
You can use strtotime to convert string to unix timestamp, and compare that.
$firstdate = strtotime("11-11-19");
$seconddate = strtotime("11-24-17");
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
You can convert both dates to timestamp:
echo (strtotime($firstdate) < strtotime($seconddate)) ? "firstdate is minor than the secondate" : "seconddate is major than the firstdate";

how to know if a date/time falls in some date/time range? [duplicate]

This question already has answers here:
PHP check if date between two dates [duplicate]
(16 answers)
Closed 6 years ago.
For example I have a date/time range which doesn't have year, and which needs to be converted to timestamp like below,
<?php
$dateStart = '11-28 00:00:00';
$dateEnd = '11-28 23:59:59';
?>
Now I want to get the current date and time of the user and check if it falls inside the given date range or not. How do I do this ?
You can use this logic:
$start_date = '2012-07-10';
$end_date = '2012-10-06';
$date_from_user = '2009-08-28';
checkInRange($start_date, $end_date, $date_from_user);
function checkInRange($start_date, $end_date, $date_from_user)
{
// Convert to timestamp
$start_timestamp = strtotime($start_date);
$end_timestamp = strtotime($end_date);
$user_timestamp = strtotime($date_from_user);
// Check that user date is between start & end
return (($user_timestamp >= $start_timestamp) && ($user_timestamp <= $end_timestamp));
}
Hope this helps!

Php subtract two dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I am getting $status_date from mysql database and when I echo $status_date it looks like this -> 2016-01-08 22:18:14. I am setting $now and when I echo it looks like this -> 2016-01-08 22:43:27. I want to subtract $status_date from $now. Here is my code:
$now = date("Y-m-d H:i:s");
$status_date = date("Y-m-d H:i:s",strtotime($row['status_date']));
$x = $now-$status_date;
$x returns this -> 0
Why is it not subracting?
Dates don't subtract like that. Convert both dates to time, subtract, and convert back to a date.
$now = strtotime(date("Y-m-d H:i:s"));
$status_date = strtotime($row['status_date']);
$x = date($now-$status_date);
However if you're trying to get the time between the dates consider the moment library.
https://github.com/fightbulc/moment.php
You can also use something like:
$now = new DateTime();
$status_date = new DateTime($row['status_date']);
$diff = $now->diff($status_date);
Then you can use any of the dateInterval properties or functions with $diff.
Documentation: http://php.net/manual/es/datetime.diff.php
http://php.net/manual/es/class.dateinterval.php
according to http://php.net/manual/en/datetime.diff.php
check this code i wrote here :
https://eval.in/499594
<?php
$strStart = '2015-01-08 22:18:14'; // status date
$strEnd = 'now';
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
print $dteDiff->format("%Y-%m-%d %H:%I:%S");
thre result is smth like : 01-0-0 00:38:42

How do I compare dates in PHP? [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 8 years ago.
How can I check if a date captured in the database is less than the current date?
foreach( $results as $r ) {
$date = $r['date'];
if($date < $currentdate) {
echo 'oldest dates';
} else {
echo 'newest dates';
}
Use strtotime function:
$date = strtotime($date);
$currentdate = strtotime($currentdate);
Use strtotime($date)<strtotime($currentdate)
and make sure that both dates are in correct format.

Categories