I have a timestamp which calculates remaining time between 2 dates.
$job_expiration = strtotime($job['job_expiration']) - time(); like so.
Right now, I'm showing only 1 day or 2 days with the date() function.
date('j', $job_expiration) like so.
I also want to show hours instead of 2 days. let's say the time remaining is 1 day 16 hours or only 30 minutes. then I want to show the hours with day too.
1 day, 16 hours or only 30 minutes
how can I do that?
function convert_seconds($seconds) {
$dt1 = new DateTime("#0");
$dt2 = new DateTime("#$seconds");
return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo convert_seconds(200000)."\n";
Sample output:
2 days, 7 hours, 33 minutes and 20 seconds
In your case you should use this function like convert_seconds($job_expiration)
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"
I was going through a php code today and found this to be really weird.
<?php
$now = new DateTime("2015-07-29 03:38:55");
$previous = new DateTime("2013-07-29 05:06:40");
$diff = $now->diff($previous);
$diff2 = $previous->diff($now);
printf("%d years, %d month, %d days, %d hours, %d minutes %d seconds.<br/>", $diff->y, $diff->m, $diff->d, $diff->h, $diff->i, $diff->s);
printf('%d years, %d month, %d days, %d hours, %d minutes %d seconds', $diff->y, $diff->m, $diff2->d, $diff2->h, $diff2->i, $diff2->s);
The output is
1 years, 11 month, 30 days, 22 hours, 32 minutes 15 seconds
1 years, 11 month, 29 days, 22 hours, 32 minutes 15 seconds
I have two questions
I read the document it said that $previous->diff($now); shows $now - $previous. However, if it is the other way around, wouldn't it be negative?
Why is one 30 days and the other is 29 days?
I have a speculation that this could be due to the invert. I'm not entirely sure why it is adding an extra day... but a quick change I did was:
$now = new DateTime("2015-07-29 03:38:55");
$previous = new DateTime("2013-07-29 05:06:40");
$diff = $now->diff($previous);
$diff2 = $previous->diff($now);
printf("%d years, %d month, %d days, %d hours, %d minutes %d seconds\r\n", $diff->y, $diff->m, ($diff->d - $diff->invert), $diff->h, $diff->i, $diff->s);
printf('%d years, %d month, %d days, %d hours, %d minutes %d seconds', $diff->y, $diff->m, ($diff2->d - $diff2->invert), $diff2->h, $diff2->i, $diff2->s);
And the output is:
1 years, 11 month, 29 days, 22 hours, 32 minutes 15 seconds
1 years, 11 month, 29 days, 22 hours, 32 minutes 15 seconds
Or, alternatively, the above is completely wrong and is just a coincidence... another explanation may be that the diff days was a float value for $diff, and so it was rounded down... yielding 29 days. Possible leap-year issue as well.
Edit
The above is also wrong... if you change the output to floats, it will show whole numbers. This adds to the confusion. I'll leave this answer to possibly help someone else figure out the solution.
$now = new DateTime("2015-07-29 03:38:55", new DateTimeZone('GMT'));
$previous = new DateTime("2013-07-29 05:06:40", new DateTimeZone('GMT'));
$diff = $now->diff($previous);
$diff2 = $previous->diff($now);
printf("%f years, %f month, %f days, %f hours, %f minutes %f seconds %f total days\r\n", $diff->y, $diff->m, $diff->d, $diff->h, $diff->i, $diff->s, $diff->days);
printf('%f years, %f month, %f days, %f hours, %f minutes %f seconds %f total days', $diff->y, $diff->m, $diff2->d, $diff2->h, $diff2->i, $diff2->s, $diff2->days);
I even set the timezone in hopes to help. Still yields whole numbers:
1.000000 years, 11.000000 month, 30.000000 days, 22.000000 hours, 32.000000 minutes 15.000000 seconds 729.000000 total days
1.000000 years, 11.000000 month, 29.000000 days, 22.000000 hours, 32.000000 minutes 15.000000 seconds 729.000000 total days
the sign of the difference is in the invert property: $diff->invert=1 and $diff2->invert=0
You can see in the php source here a day correction by 1 when:
the 1st of the dates is in DST and the 2nd is not
the timestamp of the 2nd date starts after the timetstamp of the 1st one without the DST correction
the timestamp of the 2nd date starts before the timetstamp of the 1st one with the DST correction
This is what is causing one to show 30 days and the other to show 29. In #Half Crazed's example, both dates are in DST, so no correction there. If the previous date of the OP were 2013-07-29 03:06:40, their wouldn't be a difference
I'm trying to do a simple countdown, but it's giving the wrong time:
$time_seconds_view = strtotime('tomorrow') - time(); echo date("H" . " \h\o\u\\r\s\, " . "i" . " \m\i\\n\u\\t\\e\s\, \a\\n\d " . "s" . " \s\\e\c\o\\n\d\s", $time_seconds_view);
is spitting out 00 hours, 30 minutes, and 00 seconds
even though date_default_timezone_set($this->session->userdata('timezone')); is set to America/Phoenix
it should be spitting out 07 hours, 30 minutes, and 00 seconds
it's as if PHP is not taking account that I've changed the default timezone.
Anything I'm doing wrong? Thanks for your help!
Use DateTime. It's much better for date math.
$tomorrow = new DateTime('tomorrow');
$now = new DateTime();
$diff = $tomorrow->diff($now);
echo $diff->format("%h hours, %i minutes, %s seconds");
// 7 hours, 12 minutes, 53 seconds
See in action
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