This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
If I have a given date in the format yyyy-mm-dd, how can I calculate the difference in days to the current date ?
I just want to check whether this date is more than one week (7 days) old compared to the current date.
date_default_timezone_set('Europe/Warsaw');
$from = strtotime('2013-11-01');
$today = time();
$difference = $today - $from;
echo floor($difference / 86400); // (60 * 60 * 24)
or
date_default_timezone_set('Europe/Warsaw');
$datetime1 = new DateTime('2013-11-01');
$datetime2 = new DateTime('2013-11-15');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a');
Related
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I have two dates assigned in variable $date1 and $date2..
Here is the code..
if (isset($_POST['check_in']))
{
$date1=date('Y-m-d', strtotime($_POST['check_in']));
}
if (isset($_POST['check_in']))
{
$date2=date('Y-m-d', strtotime($_POST['check_out']));
}
For example if date1="2015-05-21" and date2="2015-05-23".I want the difference of date as 2
Use DateTime class. Try with -
$date1=new DateTime("2015-05-21");
$date2=new DateTime("2015-05-23");
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');
Output
+2 days
DateTime()
Since strtotime returns unixtime, the difference in seconds can be calculated by simply subtracting the one strtotime from the other:
$seconds = strtotime($_POST['check_out']) - strtotime($_POST['check_in']);
Then to find the days:
$days = $seconds / 60 / 60 / 24;
Here you go:
https://php.net/manual/en/datetime.diff.php
Code with various examples.
Here's one I like:
<?php
$datetime1 = date_create('2015-05-21');
$datetime2 = date_create('2015-05-23');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
I hope this helps :)
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have two date's
$date1 = "2014-02-11 04:04:26 AM"
$date2 = "2014-02-11 05:36:56 AM"
I want to calculate the difference and display it as follows
1 hour 32 minutes
Make use of DateTime::diff of the DateTime Class
<?php
$datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
OUTPUT :
1 Hours 32 Minutes
Simply convert both dates to timestamp if dont want to do it in complex way...
Something like this
$dateDiff = intval((strtotime($date1)-strtotime($date2))/60);
$hours = intval($dateDiff/60);
$minutes = $dateDiff%60;
and there you go...
Thank you...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find number of days between two dates using php
Days left - subscription expire?
I have code that should echo how many days has been from date (2012-10-01) to today.
$datetime1 = date_create('2012-10-01');
$datetime2 = date_create();
$day1 = date_format($datetime1, 'Y-m-d');
$day2 = date_format($datetime2, 'Y-m-d');
$day = $day2 - $day1 / (60 * 60 * 24);
echo $day;
I cant get any right solution, so any ideas how I should do this?
Check this out Finding the number of days between two dates
Sample code (for your date) below:
$now = time();
$your_date = strtotime('2012-10-01');
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
Date Difference in php?
I have two dates in a variable like
$fdate = "2011-09-01"
$ldate = "2012-06-06"
Now I need the difference in months between them.
For example, the answer should be 10 if you calculate this from month 09 (September) to 06 (June) of next year - you'll get 10 as result.
How can I do this in PHP?
A more elegant solution is to use DateTime and DateInterval.
<?php
// #link http://www.php.net/manual/en/class.datetime.php
$d1 = new DateTime('2011-09-01');
$d2 = new DateTime('2012-06-06');
// #link http://www.php.net/manual/en/class.dateinterval.php
$interval = $d2->diff($d1);
$interval->format('%m months');
Have a look at date_diff:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%m months');
?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Date Difference in php?
Compare 2 timestamps in PHP and get the days between?
I've 2 dates like 15/02/2012 and 18/03/2012, I need to know the number of days difference between them. Does such a utility exist in php?
date_diff is what you want.
$diff = (int) ( abs(strtotime($date1) - strtotime($date2)) / (60 * 60 * 24) );
echo 'diff:' . $diff
PHP has a function for this:
http://www.php.net/manual/en/datetime.diff.php
Example
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Output
+2 days
You could turn those dates into timestamps:
$ts1 = strtotime("15-02-2012");
$ts2 = strtotime("18-03-2012");
$diff = ($ts2 - $ts1)/86400;
echo $diff . " days";