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> ';
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 have saved a date in mysql table in date('Y-m-d H:i:s') format
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
I want to add $blacklisted_days to $blacklisted_date
$result_date = $blacklisted_date + $blacklisted_days;
and then want to find the difference in days between the $result_date and $now.
$diff_days = $result_date - $now;
I believe this code block will help you solve the problem.
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
$result_date = date('Y-m-d H:i:s', strtotime($blacklisted_date . '+'. $blacklisted_days.' days'));
if( $result_date > $now ){
$datediff = strtotime($result_date) - strtotime($now);
}else{
$datediff = strtotime($now) - strtotime($result_date);
}
$diff_days = round( $datediff / (60 * 60 * 24));
You can do it easily using DateTime class, look here:
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$date1 = new DateTime($blacklisted_date); // blacklisted
$date1->add(new DateInterval("P{$blacklisted_days}D")); // add N days
$date2 = new DateTime(); // now
$interval = $date1->diff($date2); // get diff
echo $interval->days; // in days
I hope it's really clear to understand
I have two dates like
2016-01-25 07:33:54 and current date 30-01-2016 01.27.00
I want to get difference between these dates with dates in number of hours, number of minutes and number of seconds .
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$difference = strtotime($toDate)-strtotime($fromDate);
echo "Seconds : ".$difference."<br>";
echo "Minutes : ".($difference/60)."<br>";
echo "Hours : ".($difference/(60*60));
Or
A Easy way
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$datetime1 = new DateTime($toDate);
$datetime2 = new DateTime($fromDate);
$interval = $datetime1->diff($datetime2);
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
echo "$days days, $hours Hrs, $minutes Mins, $seconds Sec";
http://php.net/manual/en/function.strtotime.php
use strtotime to find time difference between two dates.
http://php.net/manual/en/class.datetime.php
You can also try this.
checkout my code below.
<?php
$datetime1 = new DateTime('2014-02-16 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes".$interval->format('%s')." Seconds";
?>
I searched online but couldn't see much help on this.
Is there a ready to use function to do this in PHP that I haven't found?
I want to calculate the number of days between 2 variable dates e.g.:
$date1 = '01/08/2014'; $date2 = '07/08/2014;
I tried the below but $count is null:
<?php
$date1 = '01/01/14';
$date2 = '07/01/14';
$count = $date1->diff($date2);
echo $count;
?>
Better:
$from = DateTime::createFromFormat("d/m/Y",$date1);
$to = DateTime::createFromFormat("d/m/Y",$date2);
$diff = $from->diff($to,true);
$days = $diff->days;
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));
Or you can even use PHP's date_diff function like this:
$interval = $date1->diff($date2);
http://php.net/manual/en/datetime.diff.php
Use DateTime::diff (aka date_diff):
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
Or:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
You can then get the interval as a integer by calling $interval->days.
I would like to add 5 days and current time to a date, which I have in string.
$date = new DateTime('2013-11-21');
date_add($date, date_interval_create_from_date_string('5 days'));
$curtime = date('H:i:s');
How to add current time to DateTime, or is there any other better way how to do it?
Just edit your last lane - I think it is the most objective solution to your problem. The rest of your code is correct.
$curtime = $date->format('Y-m-d H:i:s');
Remember that your second lane is just an alias to:
$date->add(DateInterval::createFromDateString('5 days'));
So the full code would be:
$date = new DateTime('2013-11-21');
$date->add(DateInterval::createFromDateString('5 days'));
$curtime = $date->format('Y-m-d H:i:s');
EDIT: I've just read your question again and you ask about adding current time to this date. If you want to add time, then you need to create it from current date. It's not the perfect solution, but I'm still working on it:
$now = new DateTime(date('1970-01-01 H:i:s'));
$date->add(DateInterval::createFromDateString($now->getTimestamp() . ' seconds'));
$curtime = $date->format('Y-m-d H:i:s');
echo $curtime;
EDIT2: I've corrected it much more, look at this code:
$date = new DateTime('2013-11-21');
$date->add(DateInterval::createFromDateString('5 days'));
$now = new DateTime('now');
$today = new DateTime(date('Y-m-d'));
$time = $today->diff($now);
$date->add($time);
echo $date->format('Y-m-d H:i:s');
EDIT3: And remember about time zones:
$date = new DateTime('2013-11-21', new DateTimeZone('Europe/Warsaw'));
$date->add(DateInterval::createFromDateString('5 days'));
$now = new DateTime('now', new DateTimeZone('Europe/Warsaw'));
$today = new DateTime(date('Y-m-d'), new DateTimeZone('Europe/Warsaw'));
$time = $today->diff($now);
$date->add($time);
echo $date->format('Y-m-d H:i:s');
And fiddle: http://sandbox.onlinephpfunctions.com/code/0080d18d18dd7e2fefa7dea7d961087f14ceb3df
You can add 5 days like this:
$nextX = time() + (5 * 24 * 60 * 60);
5 days * 24 hours * 60 mins * 60 secs
Try:
$date = '2013-11-21';
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
Try this
$hour_two = "2013-11-21";
$date = strtotime($hour_two);
$hour_two = $date + (5 * 24 * 60 * 60);
$hour_two=date('Y-m-d',$hour_two);
$currenttime = date('H:i:s');
$h = strtotime($currenttime);
$minute = date("i", $h);
$second = date("s", $h);
$hour = date("H", $h);
$new_time = $hour_two." ".$hour.":".$minute.":".$second;
echo $new_time."<br/>"; // here is your final date time
$timeto_string=strtotime($new_time); // test using strtotime
echo date('Y-m-d H:i:s',$timeto_string); // print by formating
$curtime = date('H:i:s');
$date = #date("Y-m-d H:i:s",strtotime($mydate.$curtime)) ;