I am trying to find the time difference between an expiry date and today's date, but my code doesn't work.
$time_one=DateTime::createFromFormat('d/m/Y', get_field('expirydate'));
$time_two=new DateTime();
$timeleft = $time_one->diff($time_two);
echo $timeleft;
Try:
echo $timeleft->format('%a days');
So when you apply diff function to DateTime object -> you receive a DateInterval object. To access its properties about total difference between dates you should apply method called format.
The diff method returns you an DateInterval object. You must format the output in order to see how many days, hours, minutes, seconds remained:
$dateTimeInTheFuture = DateTime::createFromFormat('d/m/Y', get_field('expirydate'));
$dateInterval = $dateTimeInTheFuture->diff(new DateTime());
echo 'Time remained until expire: ' . $dateInterval->format('%d days, %h hours, %i minutes, %s seconds');
Read more about DateInterval.format method
Related
i've got little problem here. I recieve reg_time from database in this format: Y-m-d H:i:s (2020-08-26 13:50:11)
and i want to compare it with current time: $today = date("Y-m-d H:i:s");
Is there any function for it? THX
The DateTime and DateInterval classes are intended for this:
<?php
$timeInput = '2020-08-26 13:50:11';
$origin = new DateTime($timeInput);
$target = new DateTime();
$interval = $origin->diff($target);
echo $interval->format('Registered %y years, %m months, %d days, %h hours, %i minutes, %s seconds ago').PHP_EOL;
There's a package called Carbon, it's very useful for date manipulation. For your use case looks like it could be solved using a method called diffForHumans()
So, the full example would be:
Carbon::parse('2020-08-26 13:50:11')->diffForHumans();
The output would be 1 day ago
Edit:
The question was fully rewritten
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.
We have an entry in database for an event time left as P1DT3H53M45S, This means 1 day, 3 hours 53 min and 45 sec. I wan to retrieve date from this format.
I can retrieve the duration left by exploding this string and the calculate and then add to current time and create date.
Is there a better way to find the duration left other than exploding ?
No need for explode. Use DateInterval instead:
$interval = new DateInterval('P1DT3H53M45S');
echo $interval->format('%d day, %h hours, %I minutes, %s seconds');
// 1 day, 3 hours, 53 minutes, 45 seconds
P1DT3H53M45S is not date, but interval. You can use DateInterval class to create a DateInterval object, from which you can format it, or add it to some of your date.
$interval = new DateInterval('P1DT3H53M45S');
print_r($interval);
# format it
echo $interval->format('%d days, %h hours, %i minutes, %s seconds');
# add it to some date
$dt = new DateTime;
$dt->add($interval);
echo $dt->format('Y-m-d H:i:s');
demo
I have a date stored into a string variable as such: Sun Jun 02 08:54:12 EDT 2013. How can I convert this into a date variable to compare the time difference between the current date and the date provided. Thanks!
Use the DateTime class to compare the current date with the timestring. Once you have the DateTime objects you can easily get the difference using the method DateTime::diff(). It will return a DateInterval object that can be printed using it's format() method.
$dt = new DateTime('Sun Jun 02 08:54:12');
$now = new DateTime();
if($now > $dt) {
$difference = $now->diff($dt);
echo $difference->format('The time is %H hours %I minutes %S seconds in the past');
} else if ($now < $dt) {
$difference = $dt->diff($now);
echo $difference->format('The time is %H hours %I minutes %S seconds in the future');
} else {
echo 'the time is now';
}
Note: Of course you'll have to extend the output in a way that it displays the difference in years, months and days additionally. I didn't that in the example, because I'm lazy ... aehhmm because the string would get too long for the example ... ;)
i've got 2 time stamps: $start_time = 1312346227; and $end_time = 1312346466;
and i am trying to substract them to see the time inbetween $end_time = $end_time - $start_time;
and i get 239.
What is the proper way of converting this to a human readable date?
if i try echo date("h:i:s A",$end_time); i get 04:03:59 and it should be 00:03:59
any ideas?
Thanks
If you have PHP 5.3, use the DateInterval class.
Example stolen from the manual page on DateInterval::format():
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
You get addiionl four hours, because of your timezone. Remember that unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC. If you (or your server) are in UTC+4 TZ, then date() will implicitly do a timezone conversion to your local time.
Solution? Use gmdate() instead
You need to set your timezone correctly. http://www.php.net/manual/en/function.date-default-timezone-set.php