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'));
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
I am using a date() format to return the starting weekday of a month. The code I have below is how I am attempting to achieve this. For the current year (2018) this works as normal. For example This month is august and the starting weekday is a Wednesday so it will return a 3 for Wednesday. (It works so far)
As we advance the year to 2019 it starts to get the starting weekday wrong.
For example January 2019 starts on a Tuesday so it should return 2 but returns 1. (one day out)
This error seems to be cumulative so if we go to 2020 then it is 2 days out etc.
I have tried so hard to format this Date() correctly but to no avail. Is this even the correct way to do this?
Code:
$future_month = 5 /*for January 2019*/
$starting_weekday = date('N',mktime(0, 0, 0, date('m', strtotime('+'.$future_month.' months', strtotime(date('Y-m-01')))), 1));
Many Thanks
Cameron
Your code makes this much more complicated than it needs to be.
$dt = new DateTime('first day of +5 months')
$dt->format('N'); // "2"
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"
I have a script that process raw CSV data and generate report grouped by week of the year.
Which looks something like this:
//timezone is set to Europe/London
$date = new DateTime($raw['order_date']); // example: 12/31/2012
$key = $date->format('Y W'); // 2012 01
$array[$key][] = $raw['product_id'];
Everything was working fine until I tried to parse data generated over new year eve, for some reason system believes that 31 December 2012 is week number 1 of year 2012.
I'm not sure is it bug or a feature, but produced reports are definitely wrong.
What is a correct way of passing this issue and keeping data grouped by weeks?
Try this:
$key = $date->format('o W');
I think the week is defined Thursday, meaning if the Thursday falls in 2013, then it's counted as being a week of that year.
$w=(int)date('W');
$m=(int)date('n');
$w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w);
$year = date('Y');
if($w==53)
{
$year=$year+1;
}
past year i am getting all d records properly but in 2016 april first select than I got april 2015 report show any thing missing or its leap year problem
In my web application, I have users input a date in a simple textbox. That input (after being sanitized, of course), is run through strtotime(), and 86399 is added to it, to make that timestamp the end of the day written (11:59:59). This is for due date purposes (so if the date passes, the application raises a flag)
For the days I tested, it worked...
January 5th saved as january 5th, at the end of the day.
March 13th saved as March 13th
March 15th saved as March 15th
March 14th, for whatever reason, saved itself as March 15th.
Is March 14th mysteriously a couple seconds short or something??
Update: Thanks to oezi for the solution - worked like a charm. Code as requested:
Old code:
if ($_POST['dateto'] != '') {
$dateto = strtotime(mysql_real_escape_string($_POST['dateto'])) + 86399;
}
New code:
# Offset to "end of day"
list($y,$m,$d) = explode('-',date("Y-m-d",strtotime($_POST['dateto'])));
$d++;
$dateto = strtotime($y . '-' . $m . '-' . $d) - 1;
March 14, 2010 is the day Daylight Saving Time begins in the United States. So if you're doing math in the local time zone, March 14 is only 23 hours long.
I would assume because this is the beginning of daylight savings time
Like others said, this is because of daylight saving time. To solve this problem, you could do this:
<?php
list($y,$m,$d) = explode('-',date("Y-m-d",strtotime($date_from_user)));
$h = 23;
$i = 59;
$s = 59;
$mytimestamp = "$y-$m-$d $h:$i:$s";
?>
What database are you using? there has to be a better way to do this (most date manipulation commands are database specific). In SQL Server, I'd just add 1 day to the date and then subtract 1 second:
DECLARE #YourDate datetime
SET #YourDate='2010-03-14'
SELECT DATEADD(ss,-1,#YourDate+1)
OUTPUT:
-----------------------
2010-03-14 23:59:59.000
(1 row(s) affected)
for what it is worth, I'd much prefer to have a condition: < NextDay than <=CurrentDay12_59_59
In all time zones that "support" daylight savings time, you'll get two days a year that don't have 24h. They'll have 25h or 23h respectively. And don't even think of hardcoding those dates. They change every year, and between time zones.
Oh, and here's a list of 34 other reasons that you hadn't thought about, and why you shouldn't do what you're doing.
http://tycho.usno.navy.mil/leapsec.html
Not all days are 86400 seconds long.
This is a rare event. And (historically) never scheduled in March.