How to find the difference in days between two dates [duplicate] - php

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to find number of days between two dates using php
If I have two dates - how do I find the real difference in days between two dates? You must take things like leap years and the number of days in each month into account.
How many days are between something like 2010-03-29 and 2009-07-16?

strtotime and simple math:
$daylen = 60*60*24;
$date1 = '2010-03-29';
$date2 = '2009-07-16';
echo (strtotime($date1)-strtotime($date2))/$daylen;

check out PHP DateTime class. It deals with all the gory details so you can just do regular subtraction.
$d1=date_create('1999-10-23');
$d2=date_create('2004-04-17');
$i=date_diff($d2,$d1);
echo $i->format('%a');

Here you go:
<?php
$date1 = strtotime("2010-03-29");
$date2 = strtotime("2009-07-16");
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days";
?>

Related

Calculate months, years and days between two given dates as timestamp [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
How to get difference between two dates in Year/Month/Week/Day?
i am trying to calculate years and months and days between two given dates in PHP.
i am also using timestamp of those date. is there any way to calculate years and months and
days from difference of those time stamp.
for example first date is 2 Jan, 2008. and second one is 5 July, 2012.
and result is 4 Years 5 monts and 3 days.
i am working on timestamp as date input and want to know that is there any function available which directly calculate above things by two input timestamp
You could use the DateTime object for that (please note the missing "," in the datetime constructor).
$datetime1 = new DateTime('2 Jan 2008');
$datetime2 = new DateTime('5 July 2012');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months and %d days');
You can do this pretty easily with DateTime:
$date1 = new DateTime("2008-01-02");
$date2 = new DateTime("2012-07-05");
$diff = $date1->diff($date2);
echo "difference " . $diff->y . " years, " . $diff->m." months, ".$diff->d." days "
Documentation
You should have a look at Carbon, it's a pretty new PHP 5.3 lib on top of DateTime with a lot of usefull methods.
For Date diff:
<?php
$dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
$dtVancouver = Carbon::createFromDate(2013, 1, 1, 'America/Vancouver');
echo $dtOttawa->diffInHours($dtVancouver);
echo $dtOttawa->diffInDays($dtVancouver);
echo $dtOttawa->diffInMinutes($dtVancouver);
echo $dtOttawa->diffInYears($dtVancouver);
If you want Human readable diff:
$dt = Carbon::createFromDate(2011, 2, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 28 days before
echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after
You can create two DateTime objects (www.php.net/datetime) from the timestamps.
When calling the diff method you get a DateInterval object, which has properties for years and months.

Work out the date 7 days ago in this format: yyyy-mm-dd [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find out what the date was 5 days ago?
I'm trying to work out the date 7 days ago from today in this format: yyyy-mm-dd.
7 is an arbitrary number.
Thanks
<?php
echo strftime('%Y:%m:%S',(strtotime('7 days ago')));
?>
First calculate the timestamp for x days ago and than create a date with php date() function.
$x = 7; //number of days in the past
$past_stamp = time() - $x*24*60*60;
$past_date = date('Y-m-d', $past_stamp);
Hope it helps.
Try this
<?php
$days_ago = 7;
$time = strftime('%Y-%m-%d',(strtotime($days_ago.' days ago')));
echo $time;
?>
Gives:
2012-10-28

Display days between one date and today [duplicate]

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

Calculate number of days remaining [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 years ago.
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
7 days remaining... 6, 5, 4, etc
Can you help me please ?
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
$days = round((timestamp_from_database - time()) / 86400);
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
http://php.net/manual/ro/function.date-diff.php

PHP: get number of days between two dates in format YYYY-MM-DD [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to find number of days between two dates using php
Is there a quick and easy way to calculate the difference in days between two date strings in this format (YYYY-MM-DD) with PHP (not MySQL)?
$date1 = new DateTime("2010-07-06"); //inclusive
$date2 = new DateTime("2010-07-09"); //exclusive
$diff = $date2->diff($date1);
echo $diff->format("%a"); //3
(PHP 5.3 and higher only)
The only solution I see for PHP < 5.2 is to loop:
strtotime("-1 days");
strtotime("-2 days");
...
strtotime("-n days");
until we get to the unix timestamp of the first date. That's conceptually, you can do it in a much more efficient way, by first guessing the number of days with the timestamp difference of the two days and then testing the neighborhood.
Why dividing by 86400 doesn't work
date_default_timezone_set("Europe/Lisbon");
$date1 = strtotime("2010-03-28");
$date2 = strtotime("2010-03-29");
echo ($date2-$date1)/86400; //gives 0.95833333333333
$date1 = strtotime("2010-10-31");
$date2 = strtotime("2010-11-01");
echo ($date2-$date1)/86400; //gives 1.0416666666667
As Gordon correctly has pointed out, dividing by 86400 would be a valid solution for this problem if the timezone was set to 'UTC' before – just don't forget to restore it to the previous value after.
You can use this function to get the number of days between two date("Y-m-d H:i:s"):
function dateDiff($dateStart, $dateEnd)
{
$start = strtotime($dateStart);
$end = strtotime($dateEnd);
$days = $end - $start;
$days = ceil($days/86400);
return $days;
}
Copied from the Duplicate I've linked below the question.
The following SO questions might be of some help:
How to calculate the date difference between 2 dates using php
Dates difference with php
Calculate the difference between date/times in PHP
Date Difference in php?
Getting the difference between two time/dates using php?
More >>

Categories