I'm calculating a timedifference with this bit of php and formatting it in days, hours and minutes.
// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
$enddate = $interval->format("%a days, %h hours, %i minutes");
// if current time is higher than expiration date set contest to finished.
if($now > $future_date) {
$enddate = 'Date ended';
}
Now I want to have the format to only display the total amount of days when it's over a day(24 hours) and start to format in hours and minutes when it's less than a day(24 hours). So it will format to hours and minutes starting from like 23 hours 59 minutes, you get the idea hopefully.
Can anyone tell me how does it done the easiest?
Do it like this:
// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
if ($now > $future_date) {
// if current time is higher than expiration date set contest to finished.
$enddate = 'Date ended';
} else if ($interval >= new DateInterval("P1D")) {
$enddate = $interval->format("%a days, %h hours, %i minutes");
} else {
$enddate = $interval->format("%a days, %h hours, %i minutes");
}
Related
I'm trying to get the difference between a date and current date but always returns 0.
$start_date = get_field('start_date', false, false); // 2021-12-15 09:00:00
$start_date = new DateTime($start_date);
$date_now = new DateTime();
if ($date_now > $start_date){
$diff = date_diff($date_now, $start_date);
}
echo $diff->format("%y Year %m Month %d Day %h Hours %i Minute %s Seconds");
The issue seems to be that $date_now is less than $start_date, so the code inside of the if block never runs. As such $diff is undefined. This should be throwing an error, which might have helped you suss this out - but sometimes error reporting is turned off. You might want to set up a local testing environment where error reporting is enabled, I'd bet you get to the bottom of this in no time. :-)
What you might be wanting is something closer to this:
$start_date = get_field('start_date', false, false); // 2021-12-15 09:00:00
$start_date = new DateTime($start_date);
$date_now = new DateTime();
$diff = date_diff($date_now, $start_date);
$time_from_str = $diff->invert ? "ago" : "until"
echo $diff->format("%y Year %m Month %d Day %h Hours %i Minute %s Seconds")
. " "
. $time_from_str;
This always gets the interval of time, but adds some context for the end user to know if the time is in the past or the future.
Or, if you want to hard-stop at the start_date (which is implied by the id statement), you could do:
$start_date = get_field('start_date', false, false); // 2021-12-15 09:00:00
$start_date = new DateTime($start_date);
$date_now = new DateTime();
if($date_now > $start_date) {
$diff = date_diff($date_now, $start_date);
} else {
$diff = new DateInterval("P0Y"); // an empty time interval, just so $diff is still defined
}
echo $diff->format("%y Year %m Month %d Day %h Hours %i Minute %s Seconds");
Hope that's helpful. Good luck!
The expiry date is in unix timestamp approx 30th sept 2020 6AM
$expiry_date_time = 160142766; //30 sept 2020 6AM
$dt1 = new DateTime();
$dt1->setTimestamp($expiry_date_time);
$now = new DateTime();
$i = $now->diff($dt1);
$time_remaining = $i->format('%y years %m months %a days %h hours %i minutes');
echo $time_remaining;
Output today when i ran the code (11th OCT 2020)
0 years 0 months 11 days 7 hours 19 minutes
This code doesn't give incorrect result but it cannot differentiate between expired and not expired, like in the above case the subscription had expired it gives the output 11 days which is not entirely incorrect because it has been 11 days since its expired if you calculate but shouldn't it be Negative 11 days ?
If I make the expiry date to two days in the future it will correctly calculate and output 2 days, xx hours and xx minutes.
How to make it differentiate between expired and not expired ?
DateInterval has an invert flag for this very purpose:
invert
Is 1 if the interval represents a negative time period and 0 otherwise.
$expiry_date_time = 160142766; //30 sept 2020 6AM
$dt1 = new DateTime();
$dt1->setTimestamp($expiry_date_time);
$now = new DateTime();
// What you're doing right now
$i = $now->diff($dt1);
var_dump($i->invert); // int(1)
// If you're doing it the other way around
$i = $dt1->diff($now);
var_dump($i->invert); // int(0)
Demo
$expiry_date_time = 160142766; //30 sept 2020 6AM
$dt1 = new DateTime();
$dt1->setTimestamp($expiry_date_time);
$now = new DateTime();
$i = $now->diff($dt1);
$time_remaining = $i->format('%y years %m months %a days %h hours %i minutes');
if($expiry_date_time - $now()->getTimestamp() < 0) {
$time_remaining = "expired $time_remaining ago";
}
echo $time_remaining;
This will add some text to the text if it is in the past, to make sure you know if it is expired or not yet.
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)
I need to calculate the remaining time (days/hours) until a certain date/time.
However, I'm not using a static date.
Imagine I have an event at 17:00 hrs on every Sunday. I need to display the time remaining until the next event, i.e. the oncoming Sunday 17:00.
I've found the following code in this answer. It works for a static date/time, but obviously isn't what I'm looking for.
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Thanks for your time.
You can use the relative time format next Sunday 17:00. Like this:
$now = new DateTime();
$future_date = new DateTime('next Sunday 17:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Output:
6 days, 2 hours, 33 minutes, 53 seconds
Read more about relative time formats here: http://www.php.net/manual/en/datetime.formats.relative.php
I'm calculating the difference between 2 dates using DateTime() and it's working fine. The problem is that I want to have the days format be able to go above a full month so 30/31 or higher.
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
$enddate = $interval->format("%m month, %d days, %h hours, %i minutes");
The current problem with this is that when I don't display the months, the days can only go up to 30/31 and anything over that will be carried over to make a new month resetting the days count with the leftover days.
I want to be able to display 42 days when the difference is 6 weeks with this kind of format:
$enddate = $interval->format("%d days, %h hours, %i minutes");
Is there a quick fix for this or do I need to manually convert the timestamp to seconds and use my own function with modulus operators for this?
You can try:
$enddate = $interval->format("%a days, %h hours, %i minutes");
See the DateInterval::format in the manual.
NOTE
Take care of the bug if you're working on windows.
This should solve your porblem:
$now = new DateTime();
$future_date = new DateTime();
// a period of 2 months
$addPeriod = new DateInterval('P2M');
// adding the period
$future_date->add($addPeriod);
// get the differnce
$interval = $future_date->diff($now);
echo($interval->days) . ' days';
For today: echo returns '61 days'
// EDIT
To avoid running into the dataInterval-Bug you can use:
$now = new DateTime();
$future_date = new DateTime();
// a period of 2 months
$addPeriod = new DateInterval('P2M');
// adding the period
$future_date->add($addPeriod);
// get the difference in second
$diffTimestamp = $future_date->getTimestamp() - $now->getTimestamp();
// convert to days
// 1 day = 86.400 seconds
$diffDays = $diffTimestamp/86400;
echo(floor($diffDays)) . ' days';
Update my php version since this is a bug in my old and it works perfectly now.
How to get aggregate days from PHP's DateTime::diff?