I have 5 fields in one of my mysql tables. 3 of them are date, time1 and time2. date is DATE data type and time1 and time2 are TIME data type. I want to calculate the interval between time1 and time2, and display in the internet browser. I tried as follows in PHP,but it didn't work.
$start_time = strtotime($row['time1']);
$end_time = strtotime($row['time2']);
$interval = $end_time - $start_time;
Do I have to combine date and time in one field and store as timestamp?
Thanks.
you can try somethig like this:
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
$interval->format($differenceFormat);
reff :http://php.net/manual/en/function.date-diff.php
You can also try this using DateTime class
$date_time1 = new DateTime('09:00:59');
$date_time2 = new DateTime('09:01:00');
$interval = $date_time1->diff($date_time2);
$interval->format('%s seconds(s)');
$start_time = strtotime($row['time1']);
$end_time = strtotime($row['time2']);
$ts1 = strtotime(str_replace('/', '-', $start_time));
$ts2 = strtotime(str_replace('/', '-', $end_time));
$diff = abs($ts1 - $ts2) / 3600;
Related
I want to result of EndTime - StartTime to get Task Completed Time in This Format Like "02 Hours : 30 Mins". Both StartTime and EndTime is in this Format "Y:m:d H:i:s". I tried this but not giving correct output.
$starttime = $row['StartTime'];
$endtime = $row['EndTime'];
//method1
$starttime = date("H:i",$starttime);
$endtime = date("H:i",$endtime);
$start = strtotime($starttime);
$end = strtotime($endtime);
$achtime = $end-$start;
$achtime = date("H:i",$achtime);
//method2
$interval = date_diff($starttime,$endtime);
$achtime = date_format($interval,'H Hours : i Mins');
I tried both but not working.
You can use DateTime Object.
$startDate = new DateTime("2018:08:18 00:00:00");
$endDate = new DateTime("2018:08:18 02:10:00");
$interval = $startDate->diff($endDate);
echo ($interval->days * 24) + $interval->h ." Hours : ". $interval->i ." Mins";
If you want to be very specific about the date being read, or the format you are being given isn't a supported format, then it is recommend to use DateTime object.
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
I need to calculate difference between 2 dates here is my dates
$start = strtotime('17/05/2016');
$end = strtotime('12/05/2016');
I have tried
echo $days_between = ceil(abs($end - $start) / 86400);
But it shows output as 17140
Hep to find the number of days between 2 given dates
You just need to specify what is the date format, so:
$date1 = DateTime::createFromFormat('d/m/Y',"12/05/2016");
$date2 = DateTime::createFromFormat('d/m/Y',"17/05/2016");
echo $diff = $date2->diff($date1)->format("%a"); //output: 5
You need to change the format:
with d/m/Y at m/d/Y
You can also use a format like this:
$s = DateTime::createFromFormat('d/m/Y', '17/05/2016');
$d = DateTime::createFromFormat('d/m/Y', '05/12/2016');
And get the difference of days:
$start = DateTime::createFromFormat('d/m/Y', '17/05/2016');
$end = DateTime::createFromFormat('d/m/Y', '12/05/2016');
$interval = $end->diff($start);
$days = $interval->format('%a');
You are using a wrong format for dates, the original is: "Y-m-d" not "d/m/Y", you can use something like this if you could change the date format:
$now = strtotime("2016-05-17"); // or your date as well
$your_date = strtotime("2016-05-12");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));
I am trying to add hh:mm:ss with the date. How can i do it?
I tried with the following but it works when the hour is string, but when adding time is similar to MySQL Date time it is not working.
$new_time = date("Y-m-d H:i:s", strtotime('+5 hours'));
I am trying to get solution for the following:
$timeA= '2015-10-09 13:40:14';
$timeB = '03:05:01'; // '0000-00-00 03:05:01'
OutPut:
$timeA + $timeB = 2015-10-09 16:45:15 ?
How Can I Add this?
Use DateInterval():
$timeA = new DateTime('2015-10-09 13:40:14');
$timeB = new DateInterval('PT3H5M1S'); // '03:05:01';
$timeA->add($timeB);
echo $timeA->format('Y-m-d H:i:s');
You would need to break your time down into the right DateInterval format but that is easily done with explode();
Here's how that might look:
$parts = array_map(function($num) {
return (int) $num;
}, explode(':', '03:05:01'));
$timeA = new DateTime('2015-10-09 13:40:14');
$timeB = new DateInterval(sprintf('PT%uH%uM%uS', $parts[0], $parts[1], $parts[2]));
$timeA->add($timeB);
echo $timeA->format('Y-m-d H:i:s');
Demo
print date('Y-m-d H:i:s',strtotime($timeA." +03 hour +05 minutes +01 seconds"));
Should work also.
So:
$timeA= '2015-10-09 13:40:14';
$timeB = vsprintf(" +%d hours +%d minutes +%d seconds", explode(':', '03:05:01'));
print date('Y-m-d H:i:s',strtotime($timeA.$timeB));
Can be the solution.
You may also convert the time into seconds with this approach from: Convert time in HH:MM:SS format to seconds only?
$time = '03:05:01';
$seconds = strtotime("1970-01-01 $time UTC");
Then you could add the seconds to
$currentTime = '2015-10-10 13:40:14';
$newTime = date("Y-m-d H:i:s", strtotime( $currentTime.'+'.$seconds.' seconds'));
If you prefer to use the DateTime objects offered by #John Conde, here are two ways to convert the time string into the format:
$formattedTime = preg_replace("/(\d{2}):(\d{2}):(\d{2})/","PT$1H$2M$3S","03:05:11");
or, as you read it from the database:
select concat(hour(last_modified),'H',minute(last_modified),'M',second(last_modified),'H') from people;
So a more general code approach would be:
$initial = 'some time';
$interval = 'the interval value';
$initialTime = new DateTime($initial);
$intervalTime = new DateInterval($interval);
$initialTime->add($intervalTime);
echo $initialTime->format('Y-m-d H:i:s');
<?php
$datetime1 = new DateTime("$da[tofollowon]");
$datetime2 = new DateTime();
$difference = $datetime1->diff($datetime2);
$days = $difference->days;
?>
Above php code displays difference in two dates, but i want to subtract these two dates datetime2 from datetime1 which should even display negative values.
Try this
$d1 = strtotime("2014-01-10"); // or your date as well
$d2 = strtotime("2014-01-01");
$datediff = $d1 - $d2;
echo floor($datediff/(60*60*24)).' days';
try
$start = strtotime('2010-01-25');
$end = strtotime('2010-02-20');
$days_between = ceil(abs($end - $start) / 86400);
For more :- Finding the number of days between two dates
$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.