I would to add a certain amount of hours to a date time using PHP.
I am interested to show the result only using hour format.
For example, I add 8 hours to a date time as follow:
$result= date("H:i", strtotime('18:00') + 8*3600);
But I got as result value 01:00, but I would get 26 as result..
Can you help me please, I could not find the solution :(
Thank you all
It's easier with DateTime:
$date = new DateTime('18:00:00');
$date->modify('+8 hours');
echo $date->format('H:i');
Edit: Maybe I don't understand the question then. If you want to get 26, then you can use something like:
$date = new DateTime('18:00:00');
echo $date->format('H') + 8;
Basic PHP dates math. strtotime() returns a unix timestamp, which is number of seconds since the epoch, midnight January 1,1970.
date() takes those timestamps and formats them into whatever representation you want. But 'H' is NOT "hours since time zero". it's "hours of the day". If you have a timestamp that represents Jul 8/2014 12 noon, then H is going to be 12, because it's noon. It's not going to be 50 bajillion hours since Jan 1/1970.
e.g.
Jul 8/2014 11pm + 3 hours = Jul 9/2014 2am.
date('H' of Jul 9/2014) = 2, not "14"
Related
Code:
$time = strtotime('2020-03-31');
echo date('Y-m-d', strtotime('-1 month', $time));
Expected Result: Any date from Feb 2020
Actual Result: 2020-03-02
Is there any better way to add or subtract a month from a given date?
Months are an awkward interval to work with, because they don't have a fixed length. Should the algorithm assume that by "1 month" you mean "30 days", or "31 days", or should it just try subtracting 1 from the "month" field in the date structure?
The last option is what is happening here: given "2020-03-31", PHP's date library is subtracting 1 from the "03" to give "2020-02-31". Since that's an invalid date (February 2020 had 29 days), it then "normalises" it to a real date - 2 days after the 29th February was the 2nd March.
Probably you want to use a more specific period to subtract, like 30 days - although note that if the initial input is "2020-03-01" that will give you "2020-01-31", not "2020-02-01".
Ultimately, this is a problem with our irregular calendar, rather than with PHP. It's really up to you to define what you mean by "a month before", and use a more specific algorithm that captures that requirement.
You can make code like below
<?php
$time = strtotime('2020-03-1 -32 days');
echo date('M-Y', $time); // output Feb-2020
?>
The above code will return date as you expected
It seems that I'm just missing something. I'm trying to convert this to time() but I'm not getting the hours and minutes.
<?php
$ss = '3:30 pm Wed 25 Mar';
echo $ss;
echo '<hr />';
echo strtotime($ss); // 1427212800
But 1427212800 translates to Wednesday, March 25th 2015, 00:00:00 (GMT +8)
How can I get the hours and minutes? Thanks
Rather than make PHP guess at what format you're trying to feed into it, use: DateTime::createFromFormat(). This gives you back a full-on DateTime object, which is handy for other things...and you get a timestamp with it as well:
echo DateTime::createFromFormat('g:i a D j M', '3:30 pm Wed 25 Mar')
->getTimeStamp(); // 1427315400
With this, you lose the ability to guess at multiple date formats, but it'll work just fine with the format you gave.
Why does this sum only 4 days when using 25-Oct-13 and 5 days (as expected) when using other dates?
<?php
echo date('d-M-y',(strtotime('25-Oct-13') + (432000)));
?>
This depends on your timezone. The last Saturday of October is the end of Daylight Saving Time (DST) in some locales. Therefore the night of October 26th to 27th in 2013 may or may not contain an extra hour.
Circumvent this issue by adding actual days instead of hours:
$myDate = new \Datetime('2013-10-25');
$myDate->add(new \DateInterval('P5D'));
This does return Oct 30th 2013.
More strange stuff can happen from incorrectly assuming that days are always exactly 24 hours.
$date = "2014-09-17";
echo date('Y-m-d', strtotime($date.' + 5 days'));
I need help.. Is this right?
Start Date: Mar 16, 2014
End Date: Mar 19, 2014
Results: 2 Days
$plantEnd = get_the_author_meta('plantEnd', $sellerID );
$plantStart = get_the_author_meta('plantStart', $sellerID );
$future = $plantEnd;
$d = new DateTime($future);
echo $d->diff(new DateTime())->format('%a').' Days';
Why does it says 2 days? Isn't it 3 days? Im confused..
Since you aren't actually using $plantStart in your code and instead using the current time, you're basically getting a difference between now (the time the script was run, on server's time zone) and the start of Mar 19, 2014 (0h:0m:0s). So what you are really getting is something like 2 days 5 hours 3 minutes 25 seconds (depending on when you run it vs. server time.
for example, when I run this locally:
$d->diff(new DateTime())->format('%d:%H:%i:%s');
I get 2:04:59:25
So there's more to it than just getting that "2" returned.. you're just not formatting for it.
And again, you aren't actually using the $plantStart anywhere either. So if you were to do this:
<?php
$plantEnd = '2014-03-19';//get_the_author_meta('plantEnd', $sellerID );
$plantStart = '2014-03-16'; //get_the_author_meta('plantStart', $sellerID );
$future = $plantEnd;
$d = new DateTime($future);
echo $d->diff(new DateTime($plantStart))->format('%d:%H:%i:%s');
?>
You will see it outputs 3:00:0:0 (or you could continue to just use %d and get the "3"). This is because $plantStart (presumably - based on your post) just specifies yyyy-mm-dd, so passing just the yyyy-mm-dd value will put the hh:mm:ss at 0:0:0 (beginning of day) , so it will be a full day's calculation, which has the effect of "rounding up" to the whole day increment.
I have a feeling that it's actually 2 days, someodd hours, and someodd minutes, or something to that effect. Because you're formatting to just do days, you're losing the nuances. I'd change the code to say "2.4 days" (and for the life of me I can't remember how I did this in the past...)
EDIT: in the past I have simply used date() instead of DateTime().
I did a little research, and you might want format('%d')." Days";
I'm dealing with an api which is returning the date time stamp exactly as follows:
Mon, 14 May 2012 14:14:11 +0000
I would like process this so php works out how many minutes ago this was if the number of minutes is less than 60 else how many hours ago it was if the number of hours is less than 24 else the number of days.
Dates will never be older than a few weeks.
Thanks.
You want to use the DateTime class. It can parse that date.
$now = new DateTime('now');
$dt = new DateTime('Mon, 14 May 2012 14:14:11 +0000');
$interval = $now->diff($dt);
$minutes = $interval->format('%i');
Note that 'now' will be in your current time zone so you might want to pass the DateTimeZone parameters as well. More info is here: http://php.net/DateTime
The class should already be built into your PHP. You won't need to include it.