See the below code
$compDate = date('d/m/y',strtotime('-2 weeks'));
echo strtotime($compDate)."-->".strtotime('-2 weeks');
The echo outputs 1398882600-->1388938336.
Why does the time stamp differ?
This is from the PHP manual:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
So change d/m/y to either d-m-y or m/d/y and strtotime will work perfectly.
Update: Yes, kingkero is right. You have to change d/m/y to either d-m-y H:i:s or m/d/y H:i:s. The point is that you can’t ignore the hour, minute and second.
The first problem is the wrong format - as stated by Sharanya Dutta. The second one is you are losing precision when formatting to m/d/y instead of m/d/y, H:i:s eg.
$compDate = date('m/d/y',strtotime('-2 weeks'));
print date("d.m.Y # H:i:s", strtotime($compDate)); //05.01.2014 # 00:00:00
print date("d.m.Y # H:i:s", strtotime('-2 weeks')); //05.01.2014 # 17:23:42
When you add hours, minutes and seconds to $compDate, it will work as expected
$compDate = date('m/d/y, H:i:s',strtotime('-2 weeks'));
var_dump( strtotime($compDate) === strtotime('-2 weeks')); // TRUE
If you don't need the time but only the date, you can set the time in the strtotime() as well.
$compDate = date('m/d/y',strtotime('-2 weeks'));
var_dump( strtotime($compDate) === strtotime('-2 weeks, 0:0:0')); // TRUE
strtotime assumes x/x/x notation is American and x-x-x notation as European. Since you are passing in d/m/Y as the formatting for $compDate and then passing that date string through strtotime() as second time, it is interpreting the date string as m/d/Y the second time. You can confirm by changing your code like this:
$compDate = strtotime('-2 weeks');
echo $compDate . "-->" . strtotime('-2 weeks');
or this way:
$compDate = date('d-m-y',strtotime('-2 weeks'));
echo strtotime($compDate) . "-->" . strtotime('-2 weeks');
or this way:
$compDate = date('m/d/y',strtotime('-2 weeks'));
echo strtotime($compDate) . "-->" . strtotime('-2 weeks');
A better solution would be to use the DateTime class, which allows you to define the date format of a given string using createFromFormat so that the parser knows which number is which:
$compDate = date('d/m/y',strtotime('-2 weeks'));
$compDateObject = DateTime::createFromFormat('d/m/y', $compDate);
echo $compDateObject->format('U') . "-->" . strtotime('-2 weeks');
That last example is a bit convoluted because generally with a relative string like -2 weeks you wouldn't also need to worry about the formatting, but I'm guessing the issue you're really having is that the date format used throughout your code is in d/m/y, so the above gives an idea of how to swap between that format and and epoch timestamp. If you want to use DateTime to get a relative date (like -2 weeks), you could revamp the above as:
$compDate = new DateTime('-2 weeks');
echo $compDate->format('U') . "-->" . strtotime('-2 weeks');
echo $compDate->format('d/m/Y');
If you want the timestamp to be for the date but don't want to use time, you can add today to the relative format, like so:
$compDate = new DateTime('2 weeks ago today');
echo $compDate->format('U') . "-->" . strtotime('-2 weeks 00:00');
echo $compDate->format('d/m/Y');
Related
I have the command date('d.m.y', strtotime('22.02.21 +1 days')). The expected output would be 23.02.21, however I am getting 26.02.21. It adds four days, not one. When trying to add two days, it adds five.
Using a different date-format (i.e. date('d.m.y', strtotime('02/22/21 +1 days'))) works, however I'd like to use this date format, if possible.
Is there a way to make strtotime() work with this format?
According to https://www.php.net/manual/en/function.strtotime.php.
strtotime expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp.
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot(.) visibly, then the European d-m-y format is assumed.
If, however, the year is given in a two digit format and the separator is a dash (-) or a dot(.) visibly, the date string is parsed as y-m-d.
date("d.m.y", strtotime("2021-02-22 +1 day"))
To avoid confusion it is better to use a date with the year on 4 digits
date('d.m.y', strtotime('22.02.2021 +1 days'))
And finally, to avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
If you are having a two digit year you need to disambiguate the century anyway. Using a four digit year will work.
In this example any year starting 70 is assumed to be in the 20th century while all other years are in the 21st century.
$inputDate = '22.02.21';
$year = substr($inputDate, 6);
if ( $year > 69 ) {
$year = '19' . $year;
} else {
$year = '20' . $year;
}
$date = substr($inputDate, 0, 6) . $year;
echo date("d.m.y", strtotime($date . ' +1 day'));
Sandbox example: http://sandbox.onlinephpfunctions.com/code/14a79178f8909f3078666ae630163fa7c943cff1
I am trying to add 1 day to a date using strtotime but I can´t get it to work. It always returns 02/01/1970
$date = date ("d/m/Y H:i:s", filemtime($directory));
$newdate = date("d/m/Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970
Because strtotime() differentiates between USA date format and Sensible date format by looking at the date seperator all you need to do is us the - seperator if you want to use the sensible date format, like this, in your intermediate date manipulations
$date = date ("d-m-Y H:i:s", filemtime($directory));
$newdate = date("d-m-Y", strtotime($date));
$tomorrow = date('d/m/Y',strtotime($newdate . "+1 days"));
echo $tomorrow; //Always return 02/01/1970
FROM the manual
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Better work with DateTime():
$date = new DateTime(strtotime(filemtime($directory)));
echo $newdate = $date->format('d/m/Y');
$date->modify('+1 day');
echo $tomorrow = $date->format('d/m/Y');
Output:
20/01/2020
21/01/2020
If your filemtime($directory) returns a string formatted as your date() mask, I mean d/m/Y H:i:s, then you can do next steps:
for example, according to this mask, it looks like:
$s = "02/06/2019 22:23:22";
now you can do strtotime()
$date = date ("d/m/Y H:i:s", strtotime($s));
then transform it to the DateTime object
$st_date = new DateTime($date);
now you can simply modify it as you want
$st_date->modify('+1 days');
to see the result string value use:
$tomorrow = $st_date->format('d/m/Y');
echo 'tomorrow -> '.$tomorrow;
Output:
date->02/06/2019 22:23:22
tomorrow -> 03/06/2019
Demo
I m trying to add 28 days to the given date and echo, however it is taking the given date and considering it as month and adding the days given
Help me fix the format.
my code goes as:
$start = "04/03/2019";
$nextpay1 = date('d/m/Y',strtotime($start . "+28 days"));
echo $nextpay1;
as result it is echoing "01/05/2019", which is not right..
I'll start my answer by quoting the strtotime docs:
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at
the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y format
is assumed. If, however, the year is given in a two digit format and
the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD)
dates or DateTime::createFromFormat() when possible.
So, in your case:
$date = DateTime::createFromFormat('d/m/Y', '04/03/2019');
$date->modify('+28 days');
echo $date->format('d/m/Y');
The problem is that $start is being parsed in the default mm/dd/yyyy format. You should use a function to parse it as the format you intend, and then add to that.
$parsed = date_parse_from_format('d/m/Y', $start);
$startdate = mktime(
$parsed['hour'],
$parsed['minute'],
$parsed['second'],
$parsed['month'],
$parsed['day'],
$parsed['year']
);
$nextpay1 = date('d/m/Y', strtotime('+28 days', $startdate));
$date=Date('y:m:d', strtotime("+3 days"));
This will save the date after 3 days
I have a date in a string format such as string(10) "30/08/2014" and i need to add a year to it. In my case i'll have 30/08/2015 I was trying to use something like this but i am failing...
$start = new DateTime($details['start_date']);
$start->add(new DateInterval('P1Y'));
$end_date = $start;
any suggestion please? thank you!
The error i get now is "DateTime::__construct(): Failed to parse time string (30/08/2014) at position 0 (3): Unexpected character"
Do I have to format the string such as Y-m-d or there is a fastest and more efficient way?
This might do the trick for you.
$end_date = strtotime(date($details['start_date'])." + 1 year");
strtotime function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp.
Yes you can add one year to that date. Example:
// you have to define the format of the date that you're feeding
$start = DateTime::createFromFormat('d/m/Y', '30/08/2014'); // create format first ($details['start_date'])
// then use the method ->add() and feed it with a DateInterval Object with the proper interval
$start->add(new DateInterval('P1Y')); // add the year
$end = $start->format('d/m/Y'); // assign it to a variable
echo $end; // 30/08/2015
convert date in Y-m-d format
$var = '20/04/2012';
$date = str_replace('/', '-', $var);
$new_date = date('Y-m-d', strtotime("+1 year", strtotime($date)));
As, PHP doesn't work well with dd/mm/yyyy format.
I hope this will help you.
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime("+1 year", strtotime($date)));
Result:
2010-05-25
The strtotime documentation reads:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator
between the various components: if the separator is a slash (/), then the
American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.),
then the European d-m-y format is assumed.
I am trying to create a function that determines the end date of an advert based on the start date and duration parameter passed by a user.
Example:
If user specify start date as 5th June 2013 and duration as 45 days:
$ad_startdate = '2013-06-05';
$ad_duration = 45;
The function should automatically get the end date which should be 20th July 2013:
$ad_end_date = '2013-07-20';
Pls note that to make it easy to generate the end date, I've assigned the variable for months a constant value which will be 30 days. Whether it's february or november or a leap year, every month has a fixed variable value of 30.
I was trying to come up with something around this but just cant figure it out.
$ad_startdate = '2013-06-05';
$ad_duration = 45;
// End date should be 2013-07-20
function getAdvertEndDate ($ad_startdate, $ad_duration){
//Add up start date with duration
$end_date = strtotime($ad_startdate) + $ad_duration;
return $end_date;
}
I have browsed through SO questions just to see if anyone has something around this but the answered ones are so different from mine challenge.
Would be very grateful getting help with this.
function getAdvertEndDate ($ad_startdate, $ad_duration){
return date("Y-m-d", strtotime($ad_startdate) + ($ad_duration * 86400));
}
Use like so:
$endDate = getAdvertEndDate("2013-04-08", 40);
PHP >= 5.3.0 Object oriented style
$date = DateTime::createFromFormat('Y-m-d', '2013-06-05');
$date->add(new DateInterval('P45D'));
echo $date->format('Y-m-d') . "\n";
Or Procedural style
$date = date_create('2013-06-05');
date_add($date, date_interval_create_from_date_string('45 days'));
echo date_format($date, 'Y-m-d');
Result:
2013-07-20
Code:
function getAdvertEndDate ($ad_startdate, $ad_duration){
$date = DateTime::createFromFormat('Y-m-d', $ad_startdate);
$date->add(new DateInterval('P'.$ad_duration.'D'));
return $date->format('Y-m-d');
}
For PHP < 5.3 use strtotime():
function getAdvertEndDate ($ad_startdate, $ad_duration){
//Add up start date with duration
return date('Y-m-d', strtotime($ad_startdate. " + $ad_duration days"));
}
echo getAdvertEndDate('2013-06-05', '45'); // 2013-07-20
http://www.php.net/manual/en/datetime.add.php
Try this code
$date = '2013-06-05';
$date1 = strtotime($date);
$date2 = strtotime('+45 day',$date1);
echo date('Y-m-d', $date2);
The native strtotime() function does this work.
Use this:
$ad_startdate = '2013-06-05';
$ad_duration = 45;
$dateArray = explode('-', $ad_startdate);
$newDate = date('Y-m-d', strtotime('+ ' . $ad_duration . ' days', mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]));
If you're using strtotime, you cant use the date format you've specified, as if using - seperators, strtotime() expects the format differently.
From PHP.net
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.