How to calculate days till some point in future in PHP? - php

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

Related

Date_Diff not returning desired result

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;

diff between two hijri dates (in days) in laravel

I want to calculate the difference in days between two hijri dates, is that possible in Laravel? i tried this code but it is not working:
<?php $diff=date_diff("1438/01/01","1439/01/01"); echo $diff->format("%R%a days"); ?>
EDIT: As I researched more about Hijri Calender, I found that it has only 354 or 355 days.. So you can take of that by subtracting 10 days. I'll be looking for more consistent solution.
Try this.. More about DateTime
<?php
$startDate = new DateTime('1438/01/01');
$endDate = new DateTime('1439/01/01');
echo ($startDate->diff($endDate))->format("%R%a");
In Laravel
use Carbon\Carbon;
then add this in your code.
$startDate = Carbon::parse('1438/01/01);
$endDate = Carbon::parse('1439/01/01);
$dateDiff = $startDate->diff($endDate)->format("%a");
echo $dateDiff;

How to calculate the number of years married?

I have a line of text as follows...
"Now married for XXX years, my wife and I are excited for the future."
Our wedding date is: June 7th, 2008
So, currently it reads:
"Now married for two years, my wife and I are excited for the future."
What php code can I put in the XXX space to automatically insert the number of years we have been married?
<?php
$married = new DateTime('2009-10-11');
$currentdate = new DateTime();
$interval = $married->diff($currentdate);
echo $interval->format('%y years');
?>
Something along those lines should do the trick. I haven't run this code, so you may need to tweak it a bit but it should get you started.
What you can do is subtract the current date and the anniversary date. use a function like one that is displayed here:
http://www.ozzu.com/programming-forum/subtracting-date-from-another-t29111.html
<?
$anniv = new DateTime('June 7th, 2008');
$now = new DateTime();
$interval = $now->diff($anniv, true);
echo 'Now married '.$interval->y.' years';
?>
The DateTime objects that people have used so far only work if you have a newer version of PHP. Here's a solution without them. Finding just the number of years is pretty easy.
<?php
$date1 = strtotime("June 7th, 2008"); //Get general timestamp for day
$today = time(); //Today's date
$secs = $today - $date1; //Get number of seconds passed since $date1 til $today
$years = floor($secs / (60*60*24*365)) //Derive years from seconds using number of seconds in a year
echo $years;
?>
or, in a single line of code:
<?php
$years = floor((time() - strtotime("June 7th, 2008")) / (60 * 60 * 24 * 365));
?>

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);

Given a time, how can I find the time one month ago

Given a time, how can I find the time one month ago.
strtotime( '-1 month', $timestamp );
http://php.net/manual/en/function.strtotime.php
In php you can use strtotime("-1 month"). Check out the documentation here: http://ca3.php.net/strtotime
We can achieve same by using PHP's modern date handling. This will require PHP 5.2 or better.
// say its "2015-11-17 03:27:22"
$dtTm = new DateTime('-1 MONTH', new DateTimeZone('America/Los_Angeles')); // first argument uses strtotime parsing
echo $dtTm->format('Y-m-d H:i:s'); // "2015-10-17 03:27:22"
Hope this adds some more info for this question.
<?php
$date = new DateTime("18-July-2008 16:30:30");
echo $date->format("d-m-Y H:i:s").'<br />';
date_sub($date, new DateInterval("P1M"));
echo '<br />'.$date->format("d-m-Y").' : 1 Month';
?>
PHP 5.2=<
$date = new DateTime(); // Return Datetime object for current time
$date->modify('-1 month'); // Modify to deduct a month (Also can use '+1 day', '-2 day', ..etc)
echo $date->format('Y-m-d'); // To set the format
Ref: http://php.net/manual/en/datetime.modify.php
This code is for getting 1 month before not 30 days
$date = "2016-03-31";
$days = date("t", strtotime($date));
echo date("Y-m-d", strtotime( "-$days days", strtotime($date) ));
These answers were driving me nuts. You can't subtract 31 days and have a sane result without skipping short months.
I'm presuming you only care about the month, not the day of the month, for a case like filtering/grouping things by year and month.
I do something like this:
$current_ym = date('ym',strtotime("-15 days",$ts));

Categories