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.
Related
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";
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I would like to convert 30.10.2014 15\:25\:24 to 30/Oct/2014 15:25:24. I tried it like this...
$s = '30.10.2014 15\:25\:24';
$date = strtotime($s);
echo date('d/M/Y H:i:s', $date);
... but my result is 01.Jan.1970 01:00:00
Its because strtotime returned FALSE , code shown below
$s = '30.10.2014 15\:25\:24'; //invalid format
$date = strtotime($s);
if($date===FALSE) //if strtotime failed
{
echo "strtotime failed";
}
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
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date format converting
I have a date like 2012-september-09. How to convert this into 2012-09-09 using PHP?
You can try with strptime
$date = '2012-september-09';
$strp = strptime($date, '%Y-%B-%d');
$ymd = sprintf('%04d-%02d-%02d', $strp['tm_year'] + 1900, $strp['tm_mon'] + 1, $strp['tm_mday']);
$new_date = new DateTime($ymd);
echo $new_date->format('Y-m-d');
here's a Codepad
Try this
Change
<?php echo date('Y-m-d',strtotime('2012-september-09'));?>
To
<?php echo date('Y-m-d',strtotime('09-september-2012'));?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate age
I'm trying to subtract the years and check the user's age. I keep getting 1969 for $user_birth, which I think the raw date can't be parsed.
$raw_birth = "01-19-1980";
$user_birth = date("Y", strtotime($raw_birth));
$today_date = date("Y", time());
echo $raw_birth."<br />".$today_date."<br />".$user_birth."<br />";
echo $today_date-$user_birth;
Any ideas?
$year="1997";
$month="01";
$day="24";
$age=date("Y");
$age-= $year;
if(($month>=date("m")) && ($day>date("d"))) {
$age--;
}
This code calculates an accurate age of an user.