How to get absolute number of months ago in PHP - php

There a number of questions regarding this, but I couldn't find the exact answer.
$datetime1 = new DateTime('2015-01-15');
$datetime2 = new DateTime(date('Y-m-d'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%m months');
As today is 2015-05-11 this returns 3 months. I would like it to always assume it's the 1st day of the month for $datetime1 so it should actually return 4 months
I supposed I could use str_replace() or some other string function to lop off the day part of $datetime1 but I'm assuming there is a more elegant method?
Thanks

If '2015-01-15' is variable. You could do:
$date = '2015-01-15';
$datetime1 = new DateTime( date('Y-m-01', strtotime($date)) );
Or use a substring function.
$date = '2015-01-15';
$datetime1 = new DateTime( substr($date, 0, 7).'-01' );
Substring would be faster but strtotime() can handle various formats.

Related

Date_Diff not returning desired result

I am a bit confused i am using date_diff to get the days difference in -/+ integer I can only get +days even though it should be -days when the 2nd date is higher than 1st date.
$datetime1 = new DateTime("2018-01-09");
$datetime2 = new DateTime("2018-09-08");
$interval = $datetime1->diff($datetime2);
dd($interval->format('%R%a days'));
result
string(9) "+242 days"
it should be -21 days since date1 is behind by date2 .
Check This output will not confuse you it will give correct output small change to your code
// for negative difference output is -31days and date should be in yy-m-d format
$datetime1 = new DateTime("2018-02-01");
$datetime2 = new DateTime("2018-01-01");
$interval = $datetime1->diff($datetime2);
printf($interval->format('%R%a days'));
echo "<br>";
// for positive difference output is +31days and date should be in yy-m-d format
$datetime3 = new DateTime("2018-01-01");
$datetime4 = new DateTime("2018-02-01");
$interval = $datetime3->diff($datetime4);
printf($interval->format('%R%a days'));
//bellow is output i get which is correct
I'm editing my answer because I think there is a typo in your question when you say -21 days and not -241 days.
So, the standard for DateTime is 'Y-m-d'.
The difference between the dates will be calculated like this:
$firstDate = new DateTime('2018-01-10');
$secondDate = new DateTime('2018-01-15');
$firstDate->diff($secondDate);
Which can be translated as:
$secondDate - $firstDate;

Calculate difference between to unix epoch date times?

I need to be able to find out the difference between two unix epoch times.
I am trying this at the moment
$interval = $nextFile-$firstFile;
($nextFile would equal "1452182820", $firstFile would equal "1452004380")
This gets me a result of "178440".
Is taking away two epoch date times away from each other valid? Or should i find the difference another way.
Try This May be help ful
<?php
$nextFile = '1452182820';
$firstFile = '1452004380';
$n = date('d-m-Y H:i:s',$nextFile);
$f = date('d-m-Y H:i:s',$firstFile);
$Date1 = date("Y-m-d", strtotime($n));
$Date2 = date("Y-m-d", strtotime($f));
$datetime1 = new DateTime($Date1);
$datetime2 = new DateTime($Date2);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Substract 2 datetimes

I need to substract 2 datetimes, I tried a lot of things but nothing seems to work:
$startime = $historial->getStarttime(); //Datetime
$endtime = $historial->getEndtime(); //Datetime
$mytime = $endtime - $startime;
I also tried with strtotime(), date()...
Any help or clue?
Look at diff() method of DateTime object: http://www.php.net/manual/en/datetime.diff.php
<?php
$interval = $startTime->diff($endTime);
It returns a DateInterval method that you can format as you want: http://www.php.net/manual/en/dateinterval.format.php
<?php
echo $interval->format('%d days');
The substraction operator doesn't work like that, you'll need DateTime::diff():
$diff = $starttime->diff($endtime);
This shall return a DateInterval instance, that you can output in whatever format you want.
$datetime1 = new \DateTime('2009-10-11');
$datetime2 = new \DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
$datetime1->sub($interval);
echo $datetime1->format("Y-m-d"); // 2009-10-09

Get absolute days away

I want to get the absolute days away a datetime is from today. For example, i would like to know if a date is 2 days away, or 78 days away, or even 5,239 days away (not likely, but you get the idea). I am using an MS SQL database which is returning datetimes where the time components are all 00:00:00.
date_diff returns relative values that you then have to do some crazy math with to get absolute dates do to calculating months, years, etc.
Also, i am having issues getting the date component only of today's date in php.
Edit:
Thanks to mr. w. This is what i ended up with:
$date = $row['AirdateDateTime'];
$today = date_create(date("Y-m-d"));
$away = date_diff($today, $date);
$d = $away->format("%R%a");
The date_create() part was the part i was originally missing to convert to an actual datetime. Also, the format needs to be %R%a. Using %R%d only works for dates in this month.
The date_diff() function (really, the DateTime::diff() method) is what you want, and it's actually not hard. In fact, I think the example in the docs is exactly what you're after:
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');
?>
or
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
?>
What's returned is a DateInterval object, which you can format any way you want with its format() method. Above, it's being formatted to days, but you have a ton of options; see http://us.php.net/manual/en/dateinterval.format.php.
You shouldn't need to do any math yourself.
[EDIT - forgot this part]
As for getting the date component of today's date, you can do something like this:
<?php
echo date('Y-m-d');
?>
See http://us.php.net/manual/en/function.date.php.
Try this
$date1 = new DateTime('2012-10-28 00:00:00',new DateTimeZone('Europe/London'));
$date2 = new DateTime('2012-10-28 03:00:00',new DateTimeZone('Europe/London'));
$interval = date_diff($date1, $date2);
$format = '%y years, %m months, %d days, %h hours, %i minutes, %s seconds, %a total days %R';
echo $interval->format($format);
if want to change the format try this
http://www.w3schools.com/php/func_date_date.asp
It's probably easiest to convert to unix timestamps (seconds since 1970) and then get the difference.
strtotime() and time() are your friends.
An alternative to what has already been suggested is the DateTime library:
$today = date("Y-m-d");
$today = new DateTime($today);
$future = new DateTime('2010-10-25');
$interval = $today->diff($future);
echo $interval->format('%d days away'); //outputs 17 days away
It sounds like what you want is
<?php
$yourDate = '2010-10-05';
echo ciel((strtotime($yourDate) - time()) / 60 / 60 / 24);

Find date difference in php?

Is there anyway to find the date difference in php? I have the input of from date 2003-10-17 and todate 2004-03-24. I need the results how many days is there within these two days. Say if 224 days, i need the output in days only.
I find the solution through mysql but i need in php. Anyone help me, Thanks in advance.
$start = new DateTime( '2003-10-17' );
$end = new DateTime( '2004-03-24' );
$diff = $start->diff( $end );
echo $diff->format( '%d days' );
...should do it.
For reference see DateTime and DateInterval.
Mind you though, this is only available as of PHP 5.3.
You can use the parse timestamp feature to convert dates to timestamps, subtract the timestamps, and then convert the resulting timestamp (seconds) to days:
floor((strtotime("2004-03-24") - strtotime("2003-10-17"))/86400);
Example is as below:
$startDate = new DateTime( '2013-04-01' ); //intialize start date
$endDate = new DateTime( '2013-04-30' ); //initialize end date
$holiday = array('2013-04-11','2013-04-25'); //this is assumed list of holiday
$interval = new DateInterval('P1D'); // set the interval as 1 day
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
if($date->format("N") <6 AND !in_array($date->format("Y-m-d"),$holiday))
$result[] = $date->format("Y-m-d");
}
echo "<pre>";print_r($result);
A detail blog is here: http://goo.gl/YOsfPX

Categories