PHP - comparing two dates [duplicate] - php

This question already has answers here:
Compare given date with today
(14 answers)
Closed 8 years ago.
I want check whether the date from variable is older than current date about 7 days or more.
Now, I check whether this date is -1 day from current:
$old_email = strtotime($result->repairs_date_received) >= strtotime('-1 day') ? true : false;

UNIX time is in seconds, so you can simply check using basic operators like > and <.
$week_ago = strtotime('a week ago');
$check = strtotime($result->repairs_date_received);
if($check > $week_ago){
// date is newer than week
} else {
// date is older than week
}

Related

How can I calcualte Age in Years, Months and Days [duplicate]

This question already has answers here:
Calculate months, years and days between two given dates as timestamp [duplicate]
(4 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 2 years ago.
Here is my code:
<?php
$userDob = $result['DoB'];
//Create a DateTime object using the user's date of birth.
$dob = new DateTime($userDob);
//We need to compare the user's date of birth with today's date.
$now = new DateTime();
//Calculate the time difference between the two dates.
$difference = $now->diff($dob);
//Get the difference in years, as we are looking for the user's age.
$age = $difference->y;
//Print it out.
echo $age;
?>
I want to output something like 50 Years, 6 Months, 20 Days but I don't know how to exactly put it as this method is only outputting number of years only.
Yes php has something built in for that :)
$age_check = "$day-$month-$year";
$today = date("Y-m-d");
$calculate = date_diff(date_create($age_check),date_create($today));
$age = $calculate->format('%y');

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

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.

Compare Dates and find out weather First date is greater from second [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 9 years ago.
Date1=23-3-2013
Date2=26-3-2013
How to compare above both date such as
If Date2 >= Date1
Echo greater them equal to
Else
Echo not greater them equal to
Here greater does not means mathematically greater
Use the function strtotime($date)
Therefore, you would have something like:
if(strtotime($date1) < strtotime($date2)){
echo Date 2 is greater than date 1;
}else{
echo Date 1 is greater or equal than Date 2;
}

PHP date function date range [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP, see if date range is partly within another date range
I have 2 dates, a start date (2011-01-01) and an end date (2011-02-28).
I have a third date that starts on 2010-01-01 and ends on 2012-03-01. This means that this third date falls within the first 2 dates range.
If the third dates start and/or end date is not within the 2 dates then it must be false.
How can I check this using php?
You can do the following:
$startDate = strtotime('2011-01-01');
$endDate = strtotime('2011-02-28');
$intervalStart = strtotime('2011-02-01');
$intervalEnd = strtotime('2012-03-01');
if ($startDate < $intervalStart || $endDate > $intervalEnd) {
echo 'Not in the interval';
} else {
echo 'In the interval';
}

Categories