How to display days and hours with date() in php - php

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)

Related

How to check if the difference between 2 dates is bigger than 15 minutes?

I am using this function to compare a date from the database to the current date, and i need to check if the difference between the 2 dates is bigger than 15 minutes but i don't know how to do that, i think i need to do something like if($comp > 0 days 0 hours 15 minutes)
function TimeOut($dateP){
$date = new DateTime(date('Y-m-d H:i:s'));
$date2 = new DateTime($dateP);
echo $comp = $date->diff($date2)->format("%d days %h hours and %i minuts %s seconds");
if ($comp > "15 minutes ?") {
return true;
}
}
You can use diff and then read the m parameter of the result. In the example below $difference will be DateInterval object:
$difference = $start_date->diff($date2);
if($difference->i > 15) {
echo "difference greater than 15 minutes"
}
A date interval stores either a fixed amount of time (in years,
months, days, hours etc) or a relative time string in the format that
DateTime's constructor supports.

Get the EXACT difference between two dates

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"

PHP DateTime->diff

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

How to fetch date from string format

We have an entry in database for an event time left as P1DT3H53M45S, This means 1 day, 3 hours 53 min and 45 sec. I wan to retrieve date from this format.
I can retrieve the duration left by exploding this string and the calculate and then add to current time and create date.
Is there a better way to find the duration left other than exploding ?
No need for explode. Use DateInterval instead:
$interval = new DateInterval('P1DT3H53M45S');
echo $interval->format('%d day, %h hours, %I minutes, %s seconds');
// 1 day, 3 hours, 53 minutes, 45 seconds
P1DT3H53M45S is not date, but interval. You can use DateInterval class to create a DateInterval object, from which you can format it, or add it to some of your date.
$interval = new DateInterval('P1DT3H53M45S');
print_r($interval);
# format it
echo $interval->format('%d days, %h hours, %i minutes, %s seconds');
# add it to some date
$dt = new DateTime;
$dt->add($interval);
echo $dt->format('Y-m-d H:i:s');
demo

Display Days/Hours Until Certain Time

I need to calculate the remaining time (days/hours) until a certain date/time.
However, I'm not using a static date.
Imagine I have an event at 17:00 hrs on every Sunday. I need to display the time remaining until the next event, i.e. the oncoming Sunday 17:00.
I've found the following code in this answer. It works for a static date/time, but obviously isn't what I'm looking for.
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Thanks for your time.
You can use the relative time format next Sunday 17:00. Like this:
$now = new DateTime();
$future_date = new DateTime('next Sunday 17:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Output:
6 days, 2 hours, 33 minutes, 53 seconds
Read more about relative time formats here: http://www.php.net/manual/en/datetime.formats.relative.php

Categories