Adding date or time in datetime format to another datetime - php

WHAT I AM DOING
I want to find the difference between 2 datetime and add it to another datetime. I am only able to get the difference in Y-m-d H:i:s format.
CODE
$begin = new DateTime($start);
$finish = new DateTime($end);
$diff = $begin->diff($finish);
$difference = $diff->format("%Y-%M-%D %H:%I:%S");
Here I want to add $difference to another datetime say $finaldate. If its not possible is there any way of getting the difference in only minutes, then i could use $date->modify("+$difference minutes");

*This is a method using DateTime:*
$begin = new DateTime($start);
$finish = new DateTime($end);
$difference = $finish->format('U') - $begin->format('U');
// working version
$minutesDiff = round(($difference/60), 0);
$finalDate = new DateTime();
$finalDate->modify(sprintf('+%s minutes', $minutesDiff));
edit
added missing bracket
edit2
version without ->diff() method

What about:
$begin = strtotime($start);
$finish= strtotime($end);
$diff = $finish-$begin;
$finaldate = strtotime($finaldate)+$diff;
echo date("Y-M-D h-i-s",$finaldate);

Related

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

Finding the current week and previous week date range in PHP

What's the simplest method to do this in PHP?
I need to make variables to be used in SQL queries.
For example, today is 06-MAY-2015.
$current_monday = '05-MAY-2014'
$current_friday = '09-MAY-2014'
$last_monday = '28-APR-2014'
$last_friday = '02-MAY-2014'
DateTime() accepts relative formats which makes this easy to do:
$current_monday = (new DateTime('Monday this week'))->format('d-M-Y');
$current_friday = (new DateTime('Friday this week'))->format('d-M-Y');
$last_monday = (new DateTime('Monday last week'))->format('d-M-Y');
$last_friday = (new DateTime('Friday last week'))->format('d-M-Y');
05-May-2014
09-May-2014
28-Apr-2014
02-May-2014
Demo
Not sure what your question was,
but perhaps you can try:
$time = time(); //today
$tmr = $time+(24*60*60);
echo date('Y-m-d', $tmr);
also:
http://www.php.net/manual/en/datetime.modify.php
see also strtotime() http://php.net/manual/en/function.strtotime.php
strtotime("+1 week");
$nextMonday = new DateTime('next monday');
$nextFriday = new DateTime('next friday');
$thisMonday = new DateTime('this monday');
$thisFriday = new DateTime('this friday');
$lastMonday = new DateTime('last monday');
$lastFriday = new DateTime('last friday');
Now to make this usable in mysql, just add ->format('Ymd')
... Eg. $thisMonday->format('Ymd');
This will return a date in the format of 20140506 which is friendly to DateTime columns in Mysql

difference between the two datetimes in hours in php

i want to calculate the hour difference between the startdatetime and enddatetime in HOURS.
The date is in format MM/DD/YYYY and TIME in HH:MM:SS format
Below is the code :
$strt_date = "03/24/2014";
$start_time = "23:14:57";
$end_date = "03/25/2014";
$end_time = "07:34:55";
$datetime1 = new DateTime($strt_date $start_time);
$datetime2 = new DateTime($end_date $end_time);
$diff = $datetime1->diff($datetime2);
$diff1 = $diff->format('%h');
Your parameters to your DateTime constructors are incorrect. If you want a space between the dates and times you need to explicitly add them.
$datetime1 = new DateTime("$strt_date $start_time");
$datetime2 = new DateTime("$end_date $end_time");
or
$datetime1 = new DateTime($strt_date . ' ' . $start_time);
$datetime2 = new DateTime($end_date . ' ' . $end_time);
$date1 = new DateTime($row['start']);
$date2 = new DateTime($row['end']);
$duration = $date2->diff($date1);
$hours = $duration->d*24 + $duration->h + $duration->i/60;
echo $hours.'<br> ';

How to calculate a date difference in calendar months?

$time_start = mktime(12,0,0,1,1,2011);
$time_end = mktime(12,0,0,7,1,2011);
$format = '%m months';
$start_date = new DateTime(date(DATE_ATOM,$time_start));
$end_date = new DateTime(date(DATE_ATOM,$time_end));
$diff = $start_date->diff($end_date, true);
echo $diff->format($format);
Outputs "5 months", I guess because it's off by an hour due to DST. However, I need to calculate the difference in calendar months; is there another class/function that will do this?
Added some fixes:
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
$time_end += (date('I',$time_end)-date('I',$time_start))*3600; // correct for DST
$start_date = new DateTime(date(DATE_ATOM,$time_start));
$end_date = new DateTime(date(DATE_ATOM,$time_end));
$start_date->modify('12pm'); // ignore time difference
$end_date->modify('12pm');
$diff = $start_date->diff($end_date);
echo $diff->format($format);
This seems to give the results I want, but I haven't fully tested it yet.
More fixes, based on Herbert's suggestions:
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
$start_date = new DateTime();
$end_date = new DateTime();
$start_date->setTimestamp($time_start);
$end_date->setTimestamp($time_end);
$diff = $start_date->diff($end_date);
$hours = $diff->format('%h');
$mins = $diff->format('%i');
$secs = $diff->format('%s');
$start_date->setTime(12,0,0);//ignore time difference for date calculations
$end_date->setTime(12,0,0);
$diff = $start_date->diff($end_date);
$years = $diff->format('%y');
$months = $diff->format('%m');
$weeks = $diff->format('%w');
$days = $diff->format('%d');
Note that the $start_date->modify('12pm') wasn't actually doing anything at all. Not sure why it didn't error.
Update
After messing around with a lot of different ideas it occurred to me that timestamps are in GMT (UTC). date(DATE_ATOM,$time_start) is applying the default timezone. However, if you set the timestamp explicitly, DateTime will assume UTC — thus, no DST problem.
The following code seems to work regardless of timezone or DST.
<?php
$time_start = mktime(12,0,0,1,1,2011);
$time_end = mktime(12,0,0,7,1,2011)
$start_date = new DateTime();
$end_date = new DateTime();
$start_date->setTimestamp($time_start);
$end_date->setTimestamp($time_end);
$diff = $start_date->diff($end_date);
$format = '%m months';
echo $diff->format($format);
?>
I tested some edge cases — both date and time, and a variety of timezones — but I haven’t tested every possibility so if you come across an issue, I’d be interested in hearing about it.

Subtracting a timestamp from present-time in PHP, express in minutes

I have a variable $minutes that expresses a point in time like this: 2011-08-31 21:02:15
How could I subtract it from the present time, and express the difference in minutes?
$minutes = (int)((time()-strtotime($minutes))/60);
Example Here
You could use the DateTime and DateInterval classes.
$now = new DateTime();
$then = new DateTime("2011-08-31 21:02:15");
$minutes = $now->diff($then)->i;
If you have a UNIX timestamp instead of a string, you can do this instead:
$now = new DateTime();
$then = new DateTime();
$then->setTimestamp(myTimestamp);
$minutes = $now->diff($then)->i;

Categories