php DateTime countdown - php

So I have a question about a DateTime in php.
$datetime1 = new DateTime('2013-02-01 10:40:00');
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');
What echo outputs is: 2day 8hours 33minutes.
Ok I know that the difference between first and the second variable is equal to the output. But is there any way that the output could be some sort of count down.
For example:
$datetime1 = new DateTime('2013-01-01 00:00:00');
$datetime2 = new DateTime('2013-01-01 13:30:00');
What I want to be output is: 13:30:00, and 2 minutes later there would be 13:28:00.
Is there any way to be done that with diff function.
Thanks for help
Sebastian

This will only work if:
one of the times in now
the page refreshes or you use ajax. PHP is executed on the server-side.
So you basically already have the code with just a tweak:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');

Related

how i can include time from database to "Days ago code below"

I have a code for days interval
<?php
$datetime1 = new DateTime(); // Today's Date/Time
$datetime2 = new DateTime('2012-07-17 15:30:17');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%D days %H hours %I minutes ago');
?>
How can I include time from database
example
<?php echo htmlentities($row['date']); ?>
I know you're using PHP, but if you want this on the front end you could consider moment.js: https://momentjs.com/ it handles human-like date outputs.''
Edit: after further searching, this is exactly what you're looking for momentPHP: https://github.com/fightbulc/moment.php

Time difference is not showing properly

Here is my code : current time - 14.32
$datetime1 = new DateTime('2015-10-12 14:34:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->i.' minutes<br>';
The output : 35 Minutes
Why is the minutes is showing 35 instead of 2 minutes?
set the time zone,
date_default_timezone_set("Asia/Kolkata");
$datetime1 = new DateTime('2015-10-12 14:34:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->i.' minutes<br>';// for current time, difference is 10 minutes.

php find total datetime difference in seconds not getting as expected

I am trying to take the differences of datetimes in php as follows:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2015-09-23 08:09:50');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%S');
echo $elapsed;
Here I am getting the difference in seconds.
I need the total time difference in seconds.
$difference = abs($datetime1->getTimestamp() - $datetime2->getTimestamp());

How to calculate the difference of datetime field and now in PHP?

I have a datetime field in my database that contains the following information:
2012-05-03 17:34:01
I want to check the difference between the datetime field and now:
$now = date("Y-m-d H:i:s");
I am attempting to work out how many days have passed between now and the time written to the database field.
How can I achieve this?
Here is the answer :)
$now = new DateTime();
$date = new DateTime("2012-05-03 17:34:01");
echo $date->diff($now)->format("%d days, %h hours and %i minutes");
$diff = abs(strtotime($date2) - strtotime($date1));
date_diff:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime("now");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Get absolute days away

I want to get the absolute days away a datetime is from today. For example, i would like to know if a date is 2 days away, or 78 days away, or even 5,239 days away (not likely, but you get the idea). I am using an MS SQL database which is returning datetimes where the time components are all 00:00:00.
date_diff returns relative values that you then have to do some crazy math with to get absolute dates do to calculating months, years, etc.
Also, i am having issues getting the date component only of today's date in php.
Edit:
Thanks to mr. w. This is what i ended up with:
$date = $row['AirdateDateTime'];
$today = date_create(date("Y-m-d"));
$away = date_diff($today, $date);
$d = $away->format("%R%a");
The date_create() part was the part i was originally missing to convert to an actual datetime. Also, the format needs to be %R%a. Using %R%d only works for dates in this month.
The date_diff() function (really, the DateTime::diff() method) is what you want, and it's actually not hard. In fact, I think the example in the docs is exactly what you're after:
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');
?>
or
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
?>
What's returned is a DateInterval object, which you can format any way you want with its format() method. Above, it's being formatted to days, but you have a ton of options; see http://us.php.net/manual/en/dateinterval.format.php.
You shouldn't need to do any math yourself.
[EDIT - forgot this part]
As for getting the date component of today's date, you can do something like this:
<?php
echo date('Y-m-d');
?>
See http://us.php.net/manual/en/function.date.php.
Try this
$date1 = new DateTime('2012-10-28 00:00:00',new DateTimeZone('Europe/London'));
$date2 = new DateTime('2012-10-28 03:00:00',new DateTimeZone('Europe/London'));
$interval = date_diff($date1, $date2);
$format = '%y years, %m months, %d days, %h hours, %i minutes, %s seconds, %a total days %R';
echo $interval->format($format);
if want to change the format try this
http://www.w3schools.com/php/func_date_date.asp
It's probably easiest to convert to unix timestamps (seconds since 1970) and then get the difference.
strtotime() and time() are your friends.
An alternative to what has already been suggested is the DateTime library:
$today = date("Y-m-d");
$today = new DateTime($today);
$future = new DateTime('2010-10-25');
$interval = $today->diff($future);
echo $interval->format('%d days away'); //outputs 17 days away
It sounds like what you want is
<?php
$yourDate = '2010-10-05';
echo ciel((strtotime($yourDate) - time()) / 60 / 60 / 24);

Categories