Sorry if this is not the correct place to post this but doing research on Google I haven't been able to find a suitable answer.
What I want to achieve is a user types in a date and time on the site and this gets inserted into the database (two different fields, a date field and a time field).
I then need to get the total of minutes until that day/time. I have the following code which prints out the days, hours and minutes. But I'm unsure of how to convert it to a total of minutes:
date_default_timezone_set("Europe/London");
$now = new DateTime();
$future_date = new DateTime(''.$Date.''.$Time.'');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours & %i minutes");
$now = new DateTime();
$future_date = new DateTime($Date.' '.$Time);
$minutes = ($future_date->getTimestamp() - $now->getTimestamp())/60;
Unix timestamp stores the date in seconds. So you can calculate it without initialize a DateInterval with the diff() function.
Related
I'm trying to know how many days have passed from a certain timestamp, but the problem is I can't set it up, so that after midnight will count it as another day.
Here is what I tried:
<?php
$now = time(); // or your date as well
$your_date = 1572123244;
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
If I put a timestamp of five minutes before midnight (1572134100), five minutes after midnight should appear that "one day passed"
The usual way of counting the days passed since a given timestamp would be something like this:
$dt = date_create_from_format('U', 1572046200);
$diff = $dt->diff(new DateTime());
echo $diff->days;
But this counts the full 24 hour periods as days. In your case you want to count calendar dates irrespective of the time of day. I would recommend then to ceil the timestamp to the midnight.
$dt = date_create_from_format('U', 1572047700);
$dt->setTime(0, 0); // set time to 00:00
$now = new DateTime('now', new DateTimeZone('UTC')); // time now, but in UTC
$now->setTime(0, 0); // set time to 00:00
$diff = $dt->diff($now);
echo $diff->days;
I am not sure about your current time zone, but timestamps are by nature in UTC, hence you should probably normalize your local time to UTC as well.
What this code does is it sets both today's date and the timestamp you are comparing against to the midnight of the UTC day and then calculates the difference between the two. Taking the time out of equation, this will always count the full 24 hour days.
$diff = strtotime(12:00:00) - strtotime(5:01:29);
echo date('H:i:s', $diff);
result is 12:01:29
Trying to get the result of 5 hrs 1 min 29 sec
I prefer to use the DateTime class for time comparison / addition / subtraction:
$dt1 = new DateTime();
$dt1->setTimestamp(strtotime('12:00:00am'));
$dt2 = new DateTime();
$dt2->setTimestamp(strtotime('5:01:29am'));
$interval = $dt1->diff($dt2);
echo $interval->format('%h hrs %i min %s sec');
Edit: with the format you gave in strtotime it was thinking 5:01am to 12pm - so it was giving a very different answer than what you wanted, so you need to specify am/pm
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?
My whole site is:
date_default_timezone_set('Europe/London');
A user can select their timezone, and in the database it saves as 4.0 (for Dubai)
Now I wish to have so the timezone the user has chosen has an impact on the countdown, that exists on the site. The countdown looks like this:
$today_date = new DateTime('now'); // now
$final_date = new DateTime($date); // a date in the future
$interval = $today_date->diff($final_date); // find the difference
$time_left = $interval->format('starting in %d days, %h hours and %i minutes'); // display it
Above this, i did following:
$userGMT = $user->get_gmt();
date_default_timezone_set($userGMT);
Which does not work correct. It still counts down for Europe/London timezone, and not the one I have chosen.
I tried do echo date('H:i:s'); and can see by the time that the above has affected and it is showing the time for the timezone I have picked.
So that works, but dateTime('now'); doesnt?
What you want to do is add the $userGMT to your currentTime and then subtract your server time. So get the time in seconds then add $userGMT*60*60 - $yourTimeZone*60*60
When the date in the future is generated, it needs to be explicit about the applicable timezone. This demonstrates that the interval is independent of the host's timezone:
<?php
$date = '2012-04-19'; // date in the future
$user_tz = new DateTimeZone('Asia/Dubai');
foreach (array('Europe/London', 'America/Los_Angeles') as $tzname)
{
date_default_timezone_set($tzname);
$today_date = new DateTime('now'); // now
$final_date = new DateTime($date, $user_tz); // future date with explicit tz
$interval = $today_date->diff($final_date); // find the difference
$time_left = $interval->format('start in %d days, %h hours and %i minutes');
echo "$tzname: $time_left\n";
}
Output:
Europe/London: start in 0 days, 11 hours and 53 minutes
America/Los_Angeles: start in 0 days, 11 hours and 53 minutes