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);
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
I am a bit confused i am using date_diff to get the days difference in -/+ integer I can only get +days even though it should be -days when the 2nd date is higher than 1st date.
$datetime1 = new DateTime("2018-01-09");
$datetime2 = new DateTime("2018-09-08");
$interval = $datetime1->diff($datetime2);
dd($interval->format('%R%a days'));
result
string(9) "+242 days"
it should be -21 days since date1 is behind by date2 .
Check This output will not confuse you it will give correct output small change to your code
// for negative difference output is -31days and date should be in yy-m-d format
$datetime1 = new DateTime("2018-02-01");
$datetime2 = new DateTime("2018-01-01");
$interval = $datetime1->diff($datetime2);
printf($interval->format('%R%a days'));
echo "<br>";
// for positive difference output is +31days and date should be in yy-m-d format
$datetime3 = new DateTime("2018-01-01");
$datetime4 = new DateTime("2018-02-01");
$interval = $datetime3->diff($datetime4);
printf($interval->format('%R%a days'));
//bellow is output i get which is correct
I'm editing my answer because I think there is a typo in your question when you say -21 days and not -241 days.
So, the standard for DateTime is 'Y-m-d'.
The difference between the dates will be calculated like this:
$firstDate = new DateTime('2018-01-10');
$secondDate = new DateTime('2018-01-15');
$firstDate->diff($secondDate);
Which can be translated as:
$secondDate - $firstDate;
I have a date and time saved in a database, in the following format: 2016-04-03 12:54:11
Basically, this date and timestamp represents the exact date and time something was created. What I'm trying to do is display a second date and time, that is the exact number of days since the first timestamp.
So if the timestamp in the dataase was 2016-04-03 12:54:11 and todays date and time is 2016-04-04 12:54:11, it would display: Overdue by: 1 Day
So far I have:
<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
$dateNow = time();
$dateDifference = abs(strtotime($dateCreated) - strtotime($dateNow));
$years = floor($dateDifference / (365*60*60*24));
$months = floor(($dateDifference - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($dateDifference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); ?>
And then:
echo "<strong>Overdue by:</strong> $days Days.";
however, this code displays that 2 dates, only a day apart, as 14 days apart.
Help would be appreciated, cheers.
Try using DateTime objects. They'll make your life much easier.
The above could then be achieved with the following:
$date1 = new DateTime('2016-04-03 12:54:11');
$date2 = new DateTime('2016-04-04 12:54:11');
$diff = $date1->diff($date2);
The $diff variable will then be a DateInterval object, which has a days property to show the amount of days between the two dates. In this case 1, so you can do:
echo "Overdue by: " . $diff->days . " days.";
Which will output Overdue by: 1 days..
On a side-note: You should really not be using mysql_ functions anymore. They have been deprecated for years and are no longer part of the PHP core in the latest PHP version. So this code will not work on an up-to-date server. Also see: Why shouldn't I use mysql_* functions in PHP?
I think the best way to do it is using the Php DateTime class witch allow us to do it.
Try this:
$datetime1 = new DateTime("YOUR_DB_TIMESTAMP");
$datetime2 = new DateTime("today");
$difference = $datetime1->diff($datetime2);
echo difference->d;
Also, you can choose the way that you want to show the diference.
$diff->format('%R%a days')
Reference:
Example-> Finding the number of days between two dates
DateTime Doc -> http://php.net/manual/es/class.datetime.php
Regards!
I would use the DateTime object for things like this, because are really a very big help.
$dateCreatedObj = DateTime::createFromFormat('Y-m-d H:i:s', $dateCreated);
$dateNowObj = new DateTime();
$dateDifference = $dateCreatedObj->diff($dateNowObj);
echo $dateDifference->format('Overdue by: %a Day');
Please note that this does not handle timezones, multilingual and plural issues.
Diff function returns the difference between two DateTimeInterface objects.
You can try to use DateTime object :
<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
$dateNow = new DateTime();
$dateOther = new DateTime($dateCreated);
$interval = $dateNow->diff($dateOther);
echo $interval->format('%R%a days');
There a number of questions regarding this, but I couldn't find the exact answer.
$datetime1 = new DateTime('2015-01-15');
$datetime2 = new DateTime(date('Y-m-d'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%m months');
As today is 2015-05-11 this returns 3 months. I would like it to always assume it's the 1st day of the month for $datetime1 so it should actually return 4 months
I supposed I could use str_replace() or some other string function to lop off the day part of $datetime1 but I'm assuming there is a more elegant method?
Thanks
If '2015-01-15' is variable. You could do:
$date = '2015-01-15';
$datetime1 = new DateTime( date('Y-m-01', strtotime($date)) );
Or use a substring function.
$date = '2015-01-15';
$datetime1 = new DateTime( substr($date, 0, 7).'-01' );
Substring would be faster but strtotime() can handle various formats.
I've a date like this: 2011-01-28 00:37:15.
What would be the most efficient way to get days from now till this date? I want like number of full days till this date so I could display message: "after X days" rather than showing ugly date.
I am using php 5.2.6.
<?
$date = "2011-01-28 00:37:15";
$date_2 = date("Y-m-d H:i:s");
$date_diff=(strtotime($date)-strtotime($date_2)) / 86400;
?>
Have a look at http://de.php.net/manual/de/datetime.diff.php (PHP >=5.3.0)
This will return you a DateIntervall wich has a public attribute days
<?php
$datetime1 = new DateTime('2011-01-28 00:37:15');
$datetime2 = new DateTime('now');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');
?>
Looks like this should help:
http://www.prettyscripts.com/code/php/php-date-difference-in-days