This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have 2 unix time, one is todays date and other is expiration time.
Todays Time: 1377173245 (2013-08-22 12:07:25)
Expiration Time: 1406303166 (2014-07-25 15:46:06)
What I want to achieve is calculate remaining time to expire the listing. In current case, it should show, 11 months -- hours -- minutes -- seconds I didn't find any good solution to calculate the difference. I doubt is it possible to calculate time difference using UNIX time system?
Thank you :-)
This is what you're looking for: DateTime::diff
First create object with 1st date:
$date = new DateTime('2013-08-22 12:07:25');
Then use the diff method:
$diff = $date->diff(new DateTime('2014-07-25 15:46:06'));
Now if you print $diff variable, you will see the array containing values for hour, minute, day and so on.
Use DateTime::diff
See the doc on https://www.php.net/manual/en/datetime.diff.php
Try using datetime::diff. Here's the example from the php.net documentation:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
It's easy to create a DateTime object using a timespamp. However, this solution requires PHP 5.3+.
Use DateTime class:
Example:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2014-07-25 15:46:06');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%M months, %H hours, %I minutes, and %S seconds remaining ');
Outputs:
11 months, 21 hours, 44 minutes, and 20 seconds remaining
It is most definitely possible. However, you'll run into problems with months, because there are different number of days in each month, so I'll just do days, hours, minutes, and seconds below.
$days=($expiration-$today)/(60*60*24);
$hours=(($expiration-$today)/(60*60))%24; //the modulus operator, gets a remainder
$minutes=(($expiration-$today)/(60))%(60*60);
$seconds=($expiration-$today)/60;
If you need months, you'll find it easier to use date_diff().
Related
I saw this response to a thread and it works but doesn't create a range between the current day and seven days time. Which is what I need. Can anyone give me a hand please?
Update:
An easier way to word it. I want to Select data between two dates. For example current day and seven days time.
My current code:
if($current_day) {
$data['current_day']=date('Y-m-d', strtotime('+7 days'));
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours, %i minutes, %s seconds");
You can use this code sample if you have php 5.3 or above,
Else try to calculate the difference of two dates in seconds using time() and strtotime(). Then translate those seconds into days/hours/minutes/seconds.
This may be a complete noob question but here goes:
I have the following code that compares two dates for absence management. Where I expect the answer to return as 2 (the difference between start and end date) I get 1.
$start_time = new DateTime("2015-01-01 00:00:00");
$end_time = new DateTime("2015-01-02 00:00:00");
$diff = $end_time->diff($start_time);
$d = $diff->days; // 1
I have also tried using just the dates (but I need the times as some absence type are done by hours not days)
Difference is 1 because there is only one day difference between both days.
To convert the datetime into hours or minutes you should look to these links:
Convert datetime into year, month, days, hours, minutes, seconds
Difference between 2 time() values
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Time diff in minutes between 2 dates
php date_diff in hours
i have:
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
this working ok, but how can i show only hours and minutes? For this example should be:
49 hours 10 minutes
From the PHP manual:
Note:
The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".
In other words, the DateInterval class won't do what you're asking for on its own; you'd have to do the calculation yourself.
Something like this should do it:
$hours = $interval->h + ($interval->d*24);
print "{$hours} hours ".$interval->format('%i minutes');
I have two dates:
$today = '2012-12-01 10:40:00';
$check = '2012-12-03 12:00:00';
How can I show countdown for this dates?
Should show me:
Count: 49 hours and 20 minutes. I can check only hours or only minutes with function mktime, but how can i compare this?
try using DateTime::diff
<?php
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
?>
You can change these dates to a unix timestamp with strtotime.
Then you can calculate the difference between them in seconds.
60 seconds in a minute, 60 minutes in an hour.
Assuming you want the clock to keep ticking as the user stays on the page, you don't really want to do that using PHP (unless you want to dispatch AJAX calls to the server every second to update the clock, which would suck). Do it client-side, using javascript.
Here are 25 pretty scripts that do that using jQuery: http://www.tripwiremagazine.com/2012/11/jquery-countdown-scripts.html
In my MySQL table I have a datatime column (the time is saved in the following format 2011-02-15 11:01:14). I extract this information with the PHP and print it to the user page.
$results = mysql_query($cmd,$link);
while ($res_array = mysql_fetch_array($results)) {
$submitted_at = $res_array['datetime'];
print $submitted_at."<br>";
}
However, I would like to show not only the given datatime, but also how long ago it was. In more detail, I need something like that: (67 day 4 hours 47 minutes 7 seconds ago). Does PHP have some functions that can make it easier. In particular I bother with the fact that month could have 28, 29, 30 and 31 days.
As an alternative, I thought about doing it on the MySQL level. It should be easy to extract the number of second from the given time to the current data. Then I can easilly transform seconds into days, hours and minutes. But I do not know how can I access this information using the mysql_fetch_array. Should it be something like $res_array['unix_timestamp(now())-unix_timestamp(datatime)']?
This'll be what you're looking for just change the formatting to suit your needs.
http://php.net/manual/en/datetime.diff.php
For example:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
Change this line:
$interval->format('%R%a days')
Following guidelines from here:
http://php.net/manual/en/function.date.php
Edit: This function is PHP 5.3+
Edit2 : Found this for older versions of PHP
How to calculate the difference between two dates using PHP?