When I attempt to format my date difference using the normal PHP codes (d,Y,m etc.) for dates and times, it simply outputs the letter, instead of the value. This is only when I format a DateTime::diff. It works fine with a simply DateTime object.
This:
$date1 = new DateTime('2000-01-01');
$date2= new DateTime('now');
$date=$date2->diff($date1);
echo $date->format('d days ago');
Outputs "d days ago".
I know that if I replace the d with a %a, it will output how many days ago this was. I was wondering what were the other characters that would output seconds, minutes, or even years.
Thanks in advance!
DateTime::diff() returns a DateInterval object.
For example:
<?php
$date1 = new DateTime('2000-01-01');
$date2= new DateTime('now');
$interval=$date2->diff($date1);
echo "Years: {$interval->y }\n";
echo "Months: {$interval->m }\n";
echo "Days: {$interval->d }\n";
echo "Hours: {$interval->h }\n";
echo "Mins: {$interval->i }\n";
echo "Secs: {$interval->s }\n";
echo $interval->format("%Y years, %m months, %d days, %H hours, %i minutes, %s seconds") . "\n";
Will output:
Years: 13
Months: 1
Days: 11
Hours: 13
Mins: 14
Secs: 44
13 years, 1 months, 11 days, 13 hours, 21 minutes, 43 seconds
Related
I am implementing a real Estate application and I need to EXACTLY check the date difference between two dates.
I will prefer for this a javascript jquery approach, but any other solution will be welcome.
My goal is to not allow users to enter dates where the difference is EXACTLY bigger than 2 (or 5) years.
I have tried, along with many others, this solution:
How to get the months between 2 dates
Unfortunately, this is not what I really need.
When I do:
The rent will start at 01-01-2017
And will end at 31-12-2018
I will get 24 months (exactly rented for 2 years), which is correct for my purpose.
But if I try:
The rent will start at 01-01-2017
And will end at 01-01-2019
I will still get 24 months (but it is rented for 2 years and 1 day), which is not correct for my purpose.
Due to the law regulations in my country (Netherlands) there is for the contracts a big difference between the two above mentioned situations.
I will appreciate any help from your side.
As mentioned in the comment, add 2 years and subtract 1 day. Best to do it with DateTime class:
$dt = DateTime::createFromFormat('d-m-Y', '01-01-2017');
$dt->add(new DateInterval('P2Y'));
$dt->sub(new DateInterval('P1D'));
var_dump($dt->format('d-m-Y'));
This will produce the desired result:
string(10) "31-12-2018"
Here's an excellent function by SunilKmCharde, from the User Contributed Notes in the PHP manual:
<?php
//////////////////////////////////////////////////////////////////////
//PARA: Date Should In YYYY-MM-DD Format
//RESULT FORMAT:
// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
// '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days
// '%m Month %d Day' => 3 Month 14 Day
// '%d Day %h Hours' => 14 Day 11 Hours
// '%d Day' => 14 Days
// '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds
// '%i Minute %s Seconds' => 49 Minute 36 Seconds
// '%h Hours => 11 Hours
// '%a Days => 468 Days
//////////////////////////////////////////////////////////////////////
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
?>
This should be accurate and be aware of leap years etc...
<?php
$date = date_create('02-01-2017');
date_add($date, date_interval_create_from_date_string('2 years'));
$twoyears = date_format($date, 'd-m-Y');
echo $twoyears; // 02-01-2019
$rentperiod = $twoyears->sub(DateInterval::createFromDateString('1 day'));
echo $rentperiod; //01-31-2019
Javascript:
function diff( d1, d2){
x = new Date(d2-d1);
return x.getUTCDate() + " " + x.getUTCMonth() + " " + (x.getUTCFullYear() - 1970);
}
example:
a = new Date('01-01-2017');
b = new Date('01-01-2019');
diff(a,b);
"Days:1, Months: 0, Years: 2"
This is a follow up to this question: Counting down days not showing the right number of days
I'm still confused about dates and times.
Setting the start and end times:
// start date: set the time of when you click the link
$startTime = strtotime('now');
$plantStart = date('M d, Y h:i:s', $startTime);
// end date: 3 days from the time of when you click the link
$date = strtotime("+3 day", $startTime);
$plantEnd = date('M d, Y h:i:s', $date);
This gives me:
Mar 17, 2014 07:33:45 (start)
Mar 20, 2014 07:33:45 (end)
Now, the problem.. when I do this:
// show how many days/hours till $plantEnd date
$d = new DateTime($plantEnd);
$daysHours = $d->diff(new DateTime())->format('%d Days, %H Hours');
echo $daysHours;
The result is always something like: 2 Days, 11 Hours its never 3 days 0 hours or 2 days 23 hours.. Is it still just getting the time till 0:00:00 on the 3rd day instead of to the exact minute of the time?
As Yohann Tilotti mention in the comments, the issue it that new DateTime() is never initialized in the diff() function.
What you probably meant was: $d->diff(new DateTime($plantStart)).
You can see a running example here: http://ideone.com/FU8akb
Like I commented, stick to one method of dates. The preferred method being DateTime:
$plantStart = new DateTime('now');
echo $plantStart->format('Y-m-d H:i:s');
// Output: 2014-03-17 12:56:00
$plantEnd = new DateTime('now + 3 days');
echo $plantEnd->format('Y-m-d H:i:s');
// Output: 2014-03-20 12:56:00
$daysHours = $plantEnd->diff(new DateTime())->format('%d Days, %H Hours');
echo $daysHours;
// Output: 3 Days, 00 Hours
$end=date_create("2013-07-30 00:30:33");
$now=date_create();
$x=date_diff($end,$now);
echo $x->format('%a days');
When I use %a it returns 45 days which is correct, when I use %d it returns 15 days. What is problem there?
Number 15 are the days calculated from difference by the months.
For example: (from http://www.php.net/manual/en/dateinterval.format.php)
<?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');
?>
The above example will output:
31 total days
1 month, 0 days
Note that date_diff($end,$now); returns DateInterval and it has its own format:
FROM PHP DOC
a = Total number of days as a result of a DateTime::diff() or (unknown) otherwise
And
d = Days, numeric
You can not have 45 days in a single month so its basically using %d or %m month %d days
45 days //or
1 month 15 days
I'm comparing dates with this code:
$date1 = new DateTime("2007-03-24 12:10:00");
$date2 = new DateTime("2009-06-26 14:00:30");
$interval = $date1->diff($date2);
If I echo this: echo $interval->m." months and".$interval->d." days."; I get the output 3 months and 2 days.. Now, I want to echo the difference between the dates but include the amount of months in the day count, so a difference of 1 month (with 30 days in it) and 5 days would be 35 days, not 1 month and 5 days. How do I do this?
I'm using PHP version 5.3+.
You should be able to use:
$interval->days;
See: http://www.php.net/manual/en/class.dateinterval.php#dateinterval.props.days
echo "There are ".$interval->days." days between the two dates.";
Your $interval variable is of type DateInterval.
Therefore, $interval->days should yeld the desired output.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP DateTime::days returns trash?
Ok, I don't get this... Could someone explain what I'm doing wrong here?
date_default_timezone_set('Europe/Oslo');
$a = new DateTime('2011-06-20 21:00:00');
$b = new DateTime('2011-06-21 05:30:00');
echo $a->format('Y-m-d H:i:s') . PHP_EOL;
echo $b->format('Y-m-d H:i:s') . PHP_EOL;
echo $a->diff($b)->format('%a days, %h hours, %i minutes and %s seconds');
echo $a->diff($b)->format('%y years, %m months, %d days, %h hours, %i minutes and %s seconds').PHP_EOL;
The output I get is:
2011-06-20 21:00:00
2011-06-21 05:30:00
6015 days, 8 hours, 30 minutes and 0 seconds
0 years, 0 months, 0 days, 8 hours, 30 minutes and 0 seconds
What is up with the days here? Shouldn't it be 0 in both cases?
It should, and it does on my system:
nanne#pustule:~$ cat test.php
<?
date_default_timezone_set('Europe/Oslo');
$a = new DateTime('2011-06-20 21:00:00');
$b = new DateTime('2011-06-21 05:30:00');
echo $a->format('Y-m-d H:i:s') . PHP_EOL;
echo $b->format('Y-m-d H:i:s') . PHP_EOL;
echo $a->diff($b)->format('%a days, %h hours, %i minutes and %s seconds');
//
nanne#pustule:~$ php test.php
2011-06-20 21:00:00
2011-06-21 05:30:00
0 days, 8 hours, 30 minutes and 0 seconds
As #pekka commented: it seems to be a bug in PHP for windows systems :D