PHP datetime comparing days - php

I'm comparing dates with this code:
$date1 = new DateTime("2007-03-24 12:10:00");
$date2 = new DateTime("2009-06-26 14:00:30");
$interval = $date1->diff($date2);
If I echo this: echo $interval->m." months and".$interval->d." days."; I get the output 3 months and 2 days.. Now, I want to echo the difference between the dates but include the amount of months in the day count, so a difference of 1 month (with 30 days in it) and 5 days would be 35 days, not 1 month and 5 days. How do I do this?
I'm using PHP version 5.3+.

You should be able to use:
$interval->days;
See: http://www.php.net/manual/en/class.dateinterval.php#dateinterval.props.days
echo "There are ".$interval->days." days between the two dates.";

Your $interval variable is of type DateInterval.
Therefore, $interval->days should yeld the desired output.

Related

Php Date subtract Date and print them custom format

I have a db that I store update date as date("d-m-Y") I would like to subtrack another date from this date.
like 01-10-2015 - 04-07-2012
I would like to print result as ex 3 years 3 mounths and 3 days ago.
How can I do that?
Here is Your need ,
<?php
$date1 = new DateTime('04-07-2012'); // old date
$date2 = new DateTime('01-10-2015'); // new date
$interval = $date1->diff($date2); // date differ function
echo $interval->format("%y years %m months %d days ago"); // formatting date
?>
OUTPUT: 3 years 2 months 27 days ago
You can also refer : Date Time Difference
get the days difference using following code :
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%y years, %m months, %d days ago");
// will print "0 years, 8 months, 27 days ago"
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
You can reffer The time format in http://php.net/manual

PHP date interval - wrong month difference

Have a look at this code:
$first = DateTime::createFromFormat('Y-m', '2001-07');
$last = DateTime::createFromFormat('Y-m', '1998-06');
$interval = $first->diff($last);
echo "m diff: ".$interval->m." y diff: ".$interval->y."\n";
The output is m diff: 0 y diff: 3
Why does it return a wrong month difference?
Interesting that if I change dates as '2001-08' and '1998-07', it returns a correct month interval ==1.
Thanks!
PHP DateTime doesn't handle incomplete datetimes.
DateTime::createFromFormat('Y-m', '2011-07') gives a DateTime that has a year of 2011, a month of 7, and a day, hour, minute and second taken from the current time (at the moment I write this, 2011-07-31 18:05:47.
Likewise, DateTime::createFromFormat('Y-m', '1998-06') gives a DateTime that has a year of 1998, a month of 6, and a day, hour, minute, and second taken from the current time. Since June 31st is a nonexistent date, the result is 1998-07-01 18:05:47 (31 days after the day before June 1st).
The difference between those two dates is then 3 years, 0 months, and 30 days.
In your example of 2001-08 and 1998-07, both months happen to have a 31st day, so the math comes out correctly. This bug is a difficult one to pin down, because it depends on the date that the code is run even though it doesn't obviously appear to.
You could probably fix your code by using a format of "Y-m-d H:i:s" and appending "-01 00:00:00" to each date you pass to createFromFormat, which will anchor the DateTime you get back to the beginning of the month.
I know this is old, maybe this may help someone out there:
$first = DateTime::createFromFormat('Y-m', '2001-07');
$last = DateTime::createFromFormat('Y-m', '1998-06');
$interval = $first->diff($last);
$num_months = (($interval->y) * 12) + ($interval->m);
Explanation : convert $interval->y which is the year to months by multiplying it by 12 and add the succeeding months which is $interval->m

date_diff outputs wrong value

$end=date_create("2013-07-30 00:30:33");
$now=date_create();
$x=date_diff($end,$now);
echo $x->format('%a days');
When I use %a it returns 45 days which is correct, when I use %d it returns 15 days. What is problem there?
Number 15 are the days calculated from difference by the months.
For example: (from http://www.php.net/manual/en/dateinterval.format.php)
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
The above example will output:
31 total days
1 month, 0 days
Note that date_diff($end,$now); returns DateInterval and it has its own format:
FROM PHP DOC
a = Total number of days as a result of a DateTime::diff() or (unknown) otherwise
And
d = Days, numeric
You can not have 45 days in a single month so its basically using %d or %m month %d days
45 days //or
1 month 15 days

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.

First day of last 3 weeks and first day of last 3 months

Am trying to calculate the first day of last 3 weeks and first day of last 3 months in unix timestamp in PHP.
I know I have to use date function but am a bit lost. I do not have PHP 5.3 thus I cannot use relative formats.
Am using the above to decide whether or not to delete a backup. e.g.
if ($time > time()-3600*24*7 && $time < time()) {
echo "Keeping: $file<br/>";
}
I want to keep backups for:
Last 7 days
First day of last 3 weeks
First day of last 3 months
Am trying to calculate the first day of last 3 weeks and first day of last 3 months in unix timestamp in PHP.
I know I have to use date function but am a bit lost. I do not have PHP 5.3 thus I cannot use relative formats.
Am using the above to decide whether or not to delete a backup. e.g.
if ($time > time()-3600*24*7 && $time < time()) {
echo "Keeping: $file<br/>";
}
I want to keep backups for:
Last 7 days
First day of last 3 weeks
First day of last 3 months
Update
Adding the solution, as I figured it out with the help of Pekka
$a = (strtotime("last Monday-1 week"));
$b = (strtotime("last Monday-2 week"));
$c = (strtotime("last Monday-3 week"));
$d = (strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
$e = (strtotime('-1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')));
$f = (strtotime('-2 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')));
I know this question quite old, but thought of sharing my own answer so that other people might find it useful. This worked in PHP 7.2 and not sure about other versions. To get first day of 3 months back, here is the code.
echo date('Y-m-d', strtotime("FIRST DAY OF -3 MONTH"));
result:
2021-01-01
Use strtotime()'s magic.
echo strtotime("-7 days");
echo strtotime("-3 weeks");
echo strtotime("-3 months");
strtotime() can even do stuff like last tuesday, midnight.....
To calculate dates you can use strtotime:
$last7days = strtotime('-7 days');
$last3weeks = strtotime('-3 weeks');
$last3months = strtotime('-3 months');
then simply compare it to $time value:
if ( $time > $last7days ) {
...
}

Categories