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";
?>
Related
I need help to subtract 2 times in PHP.
Example:
$date1 = 02:40;
$date2 = 00:00;
$finaldate = $date1 - $date2;
the correct answer will be 21:20.
Check it out:
if use $date2 as 24:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('24:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); // 21:20
But if use $date2 as 00:00
$date1 = new DateTime('02:40');
$date2 = new DateTime('00:00');
$finaldate = $date2->diff($date1);
echo $finaldate->format('%h:%i'); //02:40
use strtotime() and subtract both the times ...
<?php
$date1=strtotime("02:40");
$date2=strtotime("00:00");
$diff= date('H:i', $date2-$date1);
echo "Time Difference : ".$diff;
?>
This will output:
Time Difference : 21:20
I'm trying to get remaining Days, hours and minutes to a certain date using php.
However i get a very strange output from my code which looks like this:
-16828 days and -11 hours and -21 minutes and -24 seconds
The future dates are stored in the mysql database in this format:
29/01/2016 7pm
So I went ahead and done this:
$Draw_time = "29/01/2016 7pm";
$date = $Draw_time;
$timestamp = strtotime($date);
$new_date = date('Y-m-d a',$timestamp );
$seconds = strtotime($new_date) - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
echo "$days days and $hours hours and $minutes minutes and $seconds seconds";
But when i run this code, I get the above strange output!
I understand that this could be because of a number reasons but the only thing i could think of is the fact that I am using a in my format?
Could someone please advise on this issue?
Simply use DateTime class like as
$Draw_time = "29/01/2016 7pm";
$date = DateTime::createFromFormat("d/m/Y ha",$Draw_time);
$date2 = new DateTime();
echo $diff = $date2->diff($date)->format("%a days and %H hours and %i minutes and %s seconds");
Try this
<?php
$Draw_time = str_replace('/', '-', "29/01/2016 7pm");
$now = new DateTime();
$futureDate = new DateTime($Draw_time);
$interval = $futureDate->diff($now);
echo $interval->format("%a days %h hours %i minutes %s seconds");
?>
Try this.
$draw_time = "2016/01/29 7pm";
$date_time = explode(" ", $draw_time);// make separate date and time in array
$date = strtotime($date_time[0]); // convert your date(2016/01/29) into php time
$time = strtotime($date_time[1]); // convert your time(7pm) into php time
$date = $date + $time; // make total time to count
$new_Date = $date - (time()); // convert into difference from current time
$day = $new_Date % 86400;
$hrs = $new_Date % 3600;
$min = $new_Date % 60;
echo "Day= ".(date("d",$day));
echo " Hours= ".(date("h",$hrs));
echo " Minutes= ".(date("i",$min));
I want to get the time difference between 2 dates in minutes. The 2 dates are in the following format
date1='05-11-2012 11:25:00'
date2='06-11-2012 17:45:00'
$datetime1 = new DateTime('05-11-2012 11:25:00');
$datetime2 = new DateTime('06-11-2012 17:45:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); //return +1 days
//or
$datetime1 = date_create('05-11-2012 11:25:00');
$datetime2 = date_create('06-11-2012 17:45:00');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); //return +1 days
for php <5.3
$date1 = '05-11-2012 11:25:00';
$date2 = '06-11-2012 17:45:00';
$diff = floor(abs( strtotime( $date1 ) - strtotime( $date2 ) )/(60*60*24));
printf("%d days\n", $diff); //return 1 days
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
For further reference please visit the following link
http://php.net/manual/en/datetime.diff.php
You neglected to indicate what format you'd like the time difference expressed in. If you're willing to work in seconds (which are easily converted), you could simply convert each date to a timestamp and then take the absolute difference using basic math. For instance, give $date1 = '05-11-2012 11:25:00' and $date2 = '06-11-2012 17:45:00':
$diff_seconds = abs( strtotime( $date1 ) - strtotime( $date2 ) );
And you could do whatever you like with the result.
$start_time = strtotime( "2012-10-12 11:35:00" );
$end_time = strtotime( "2012-10-13 12:42:50" );
echo round ( abs( $end_time - $start_time ) / 60,2 ). " minute";
This is different between two dates in MINUTES.
I want to subtract a date in the future from todays date, I want it to display both the day, hour and minutes until the target date.
Here is my code
<?php
date_default_timezone_set('Europe/London');
$format = "h:i d";
$date = date($format);
$target = date($format, mktime(0,0,0,12,8,2011));
echo date($format, $target-$date);
?>
Kind regards,
Adam
Don't use date for operations, it's meant for displaying dates. Instead subtract 2 timestamps:
...
$date = mktime(now...);
$target = mktime(0,0,0,12,8,2011);
echo date($format, $target - $date);
But you have to realize timestamps start in 1970 and end in 2038, therefore, for example, 2011 - 2007 = 1974.
More suitable in your case would be date_diff as #Kerrek SB suggested in comment.
Example (from php.net):
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); // +2 days
Maybe this will help you
<?php
$target = mktime (23, 34, 0, 9, 28, 2009);
$today = mktime();
$difference =($target-$today) ;
$days = $difference / 3600 / 24;
$difference = $difference - floor($days) * 3600 * 24;
$hours = $difference / 3600;
$difference = $difference - floor($hours) * 3600;
$minutes = $difference / 60;
echo "Days: ";
echo floor($days);
echo "<br />";
echo "Hours: ";
echo floor($hours);
echo "<br />";
echo "Minutes: ";
echo floor($minutes);
?>
I am wanting to find out the time difference in minutes between two dates which is in the format d-m-Y H:i (14-04-2009 12:15) using php?
Parse the times into timestamps using strtotime() and then simply subtract one from the other.
After that you can get the number of minutes, days and so on by using math functions.
For example:
// $date1 and $date2 are given
// the difference is in seconds
$difference = strtotime($date1) - strtotime($date2);
// getting the difference in minutes
$difference_in_minutes = $difference / 60;
Reference: strtotime()
date_default_timezone_set('Asia/Kolkata');
$currentDateTime = date('m/d/Y H:i:s');
$model_current_time = date('Y-m-d H:i:s',
strtotime($currentDateTime));
echo $model_current_time."------";
$date = DateTime::createFromFormat('d/m/Y h:i:s A',
$row['model_creation_time']);//get from resouses
$new_date_format = $date->format('m/d/Y H:i:s');
$model_creation_time = date('Y-m-d H:i:s',
strtotime($new_date_format));
echo $model_creation_time;
$datetime1 = new DateTime($model_current_time);
$datetime2 = new DateTime($model_creation_time);
$interval = $datetime1->diff($datetime2);
echo $interval->d;
echo $interval->h;
echo $interval->s;