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

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";

Related

How to convert year format YY into YYYY in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert year(YY) to YYYY. Here i am try to use date function in PHP but not getting the expected result.
$expiry_year = "30"; //year=2030
echo date('Y',strtotime($expiry_year)); //Result: 1970
echo date('Y',$expiry_year); //Result: 1970
Expected Result: 2030
Thanks To All!
Try this, use createFromFormat
$date = "30";
$dates = DateTime::createFromFormat('y', $date);
//now to get the outpu:
$arr = $dates->format('Y'); // output : 2030
DEMO
Try this :
$date = DateTime::createFromFormat('y', '30');
echo $date->format('Y');
Note:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
For this you can use date_create_from_format Alias of the DateTime class createFromFormat method like as
$expiry_year = "30";
$date = date_create_from_format('y',$expiry_year);
echo $date->format('Y'); //2030
Docs
$expiry_year = "30";
$ce = substr(date('Y'),0,2); // Check the century
$ny = substr(date('Y'),2,2); // Check current year (2 digits)
if($expiry_year < $ny) {
$next_ce = $ce+1;
$output = $next_ce.$expiry_year;
} else {
$output = $ce.$expiry_year;
}
Something like that ? :D

PHP if condition based on date not working [duplicate]

This question already has answers here:
Compare given date with today
(14 answers)
Closed 8 years ago.
I want to execute a if statement where I want if given {date} is less or equal than today then {due_amount}= {paidbystudent} else {due_amount}= '0'
Here is my code:
if ({date} <= Today()) {
{due_amount}={paidbystudent};
} else {
{due_amount}='0';
}
But its not working. Please help me how to do this.
I have also tried this code but its not working properly this is checking only date and ignoring month and year
$todays_date=date("d/m/Y", strtotime("today"));
$date=date("d/m/Y", strtotime({date}));
if ($date <= $todays_date) {
{due_amount}={paidbystudent};
} else {
{due_amount}='0';
}
How about this:
$date = '2014-05-22'; // fetch from db, user input etc
if (strtotime($date) <= time()) {
echo 'paid by student';
} else {
echo 0;
}
DEMO
EDIT: As pointed out by deceze, a better way to approach this would be to compare the UNIX timestamp values instead of a date format.

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.

PHP - How to find if a date is between 2 dates [duplicate]

This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 9 years ago.
I get some datas with dates from a database :
$data['my_date'])
Type is date : YYYY-MM-DD
I would like to make with PHP a condition to found if $data['my_date']) is between 2 dates like :
if ($data['my_date'] >= 2009-01-01 && $data['my_date'] <= 2009-12-31) {
$datas = '5';
}
else {
$datas = 1;
}
It doesn't work as expected, the condition is not verified. What should i do ?
There is no literal syntax for dates, you should enclose them in quotes and treat them as strings. If you are using the YYYY-MM-DD format, then it is sortable alphabetically, and your current method should work.
if ($data['my_date'] >= '2009-01-01' && $data['my_date'] <= '2009-12-31') {
$datas = '5';
}
else {
$datas = 1;
}
If you don't quote your dates, you are doing integer operations:
$data['my_date'] >= 2009-01-01 ====> $data['my_date'] >= 2007
You need to convert all dates by strtotime() to timestamp and then compare them.
strtotime($data['my_date']) >= strtotime('2009-01-01')...
You use either strtotime() or mktime() to convert your dates into timestamp, then compare it.

Comparing two date in php [duplicate]

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
}

Categories