I have a db that I store update date as date("d-m-Y") I would like to subtrack another date from this date.
like 01-10-2015 - 04-07-2012
I would like to print result as ex 3 years 3 mounths and 3 days ago.
How can I do that?
Here is Your need ,
<?php
$date1 = new DateTime('04-07-2012'); // old date
$date2 = new DateTime('01-10-2015'); // new date
$interval = $date1->diff($date2); // date differ function
echo $interval->format("%y years %m months %d days ago"); // formatting date
?>
OUTPUT: 3 years 2 months 27 days ago
You can also refer : Date Time Difference
get the days difference using following code :
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%y years, %m months, %d days ago");
// will print "0 years, 8 months, 27 days ago"
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
You can reffer The time format in http://php.net/manual
Related
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 4 years ago.
how can I get difference between 2 time in hours.
For ex:
$data1 = '2018-04-24 02:30:00';
$date2 = now();
how to get diff between $date1 and $date2.
EDIT: code posted by OP in comments
<?php //date_default_timezone_set('UTC+6');
$time1 = strtotime('2018-04-25 12:00:00');
$time2 = time();
echo $time2.'<br>';
echo date('Y-m-d h:i:s', $time2).'<br>';
echo ($time1-$time2)/3600; ?>
<?php
$datetime1 = new DateTime('2018-04-24 02:30:00');
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y %m %d %H:%I:%S');
The result will be:
00 0 1 08:06:15
00 --> years
0 --> months
1 --> days
08 --> hours
06 --> minutes
15 --> seconds
You can modify it as you like but i suggest you keep at least days cause the hours may differ by a few hours but the days can differ by many days.
$data1 = '2018-04-24 02:30:00';
$data2 = date('y-m-d H:i:s');
$formated_in = date('Y-m-d H:i:s', strtotime($data1));
$formated_out = date('Y-m-d H:i:s', strtotime($data2));
$formated_new_in = strtotime($formated_in);
$formated_new_out = strtotime($formated_out);
$sub_total = $formated_new_out - $formated_new_in;
$sub_total = gmdate("H:i", $sub_total);
echo $sub_total;
I have this timestamp from the database 1496592689
the problem is I don't know how to get the remaining days, hours and minutes
but I have this code bellow
this is my variable from where the timestamp stored $db_user_timestamp
and now I have this current time now $timenow = time();
I tried to calculate it with
$remainingtime = $db_user_timestamp - $timenow;
But I don't know how to put it in the days, hours and minutes.
Thanks in advance for helping me :)
Always use DateTime
$create_time = "1496592689";
$current_time = time();
$dtCurrent = DateTime::createFromFormat('U', $current_time);
$dtCreate = DateTime::createFromFormat('U', $create_time);
$diff = $dtCurrent->diff($dtCreate);
$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);
echo $interval;
result
6 months 30 days 5 hours 52 minutes
If your PHP version is 5.3 or latest, you should check
http://php.net/manual/en/class.dateinterval.php
and
http://php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime(date('Y-m-d H:i:s', $db_user_timestamp));
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months %d days %h hours %m minutes %s seconds');
<?php
$db_user_timestamp = 1496592689;
$difference = $db_user_timestamp - time();
echo "Day : ".$day = date('d',$difference);
echo "<br>Hour : ".$hour = date('H',$difference);
echo "<br>Minute : ".$minute = date('i',$difference);
?>
Using date('M/d/Y H:i:s', $theTimestamp); will give you date in day, month, year, hour, minute, seconds in the order you want (change 'M/d/Y H:i:s' to your string depending on http://php.net/manual/en/function.date.php)
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have two timestamps let us say
$end_date = 2014-09-09 15:03:10 and now date
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
I want to calculate number of days remaining .Suppose if that particular date crosses now date and it should display remaining days with -ve value.
I am using the following code
$remaining_days =strtotime($end_date) - strtotime($now) ;
$Result_days = floor($remaining_days /86400);
echo $remaining_days.' '.$Result_days.'<br/>'
Problem is that if the end date = today's date it is displaying -1 . I want to calculate based on time and display remaining days and hours.
Please help me to find out the solution.
Try this:
<?php
$end_date = "2014-10-09 15:03:10";
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
$diff = strtotime($now) - strtotime($end_date);
$fullDays = floor($diff/(60*60*24));
$fullHours = floor(($diff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($diff-($fullDays*60*60*24)-($fullHours*60*60))/60);
echo "Difference is $fullDays days, $fullHours hours and $fullMinutes minutes.";
Output:
Difference is -30 days, 0 hours and 39 minutes.
Demo:
http://3v4l.org/3auqe
Edit (using DATE OBJECT):
<?php
// Example 1
$end_date = "2014-09-11 20:35:10";
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
$date1=date_create($now);
$date2=date_create($end_date);
$diff=date_diff($date1,$date2,FALSE);
echo $diff->format("%R%d days, %h hours, %m minutes, %s seconds").PHP_EOL;
//Output:
+2 days, 3 hours, 0 minutes, 44 seconds
// Example 2
$end_date = "2014-09-08 20:35:10";
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
$date1=date_create($now);
$date2=date_create($end_date);
$diff=date_diff($date1,$date2,FALSE);
echo $diff->format("%R%d days, %h hours, %m minutes, %s seconds").PHP_EOL;
//Output:
-0 days, 20 hours, 0 minutes, 16 seconds
Demo:
http://3v4l.org/dPSgX#vhhvm-320
you can use date_diff php method .you can see example here http://php.net/manual/en/function.date-diff.php
You may try like this:
<?php
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes.";
?>
See the Source for more options
$date1 = new DateTime("2014-09-09");
$date2 = new DateTime("2014-09-12");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
The duplicate
I'm comparing dates with this code:
$date1 = new DateTime("2007-03-24 12:10:00");
$date2 = new DateTime("2009-06-26 14:00:30");
$interval = $date1->diff($date2);
If I echo this: echo $interval->m." months and".$interval->d." days."; I get the output 3 months and 2 days.. Now, I want to echo the difference between the dates but include the amount of months in the day count, so a difference of 1 month (with 30 days in it) and 5 days would be 35 days, not 1 month and 5 days. How do I do this?
I'm using PHP version 5.3+.
You should be able to use:
$interval->days;
See: http://www.php.net/manual/en/class.dateinterval.php#dateinterval.props.days
echo "There are ".$interval->days." days between the two dates.";
Your $interval variable is of type DateInterval.
Therefore, $interval->days should yeld the desired output.
This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 years ago.
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
7 days remaining... 6, 5, 4, etc
Can you help me please ?
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
$days = round((timestamp_from_database - time()) / 86400);
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
http://php.net/manual/ro/function.date-diff.php