I am feeling a bit thick today and maybe a little tired..
I am trying to add days on to a string date...
$startdate = "18/7/2011";
$enddate = date(strtotime($startdate) . " +1 day");
echo $startdate;
echo $enddate;
My heads not with it... where am i going wrong ?
Thanks
Lee
Either
$enddate = date(strtotime("+1 day", strtotime($startdate)));
or
$enddate = date(strtotime($startdate . "+1 day"));
should work. However, neither is working with the 18/7/2011 date. They work fine with 7/18/2011: http://codepad.viper-7.com/IDS0gI . Might be some localization problem.
In the first way, using the second parameter to strtotime says to add one day relative to that date. In the second way, strtotime figures everything out. But apparently only if the date is in the USA's date format, or in the other format using dashes: http://codepad.viper-7.com/SKJ49r
try this one, (tested and worked fine)
date('d-m-Y',strtotime($startdate . ' +1 day'));
date('d-m-Y',strtotime($startdate . ' +2 day'));
date('d-m-Y',strtotime($startdate . ' +3 day'));
date('d-m-Y',strtotime($startdate . ' +30 day'));
first parameter of date() is format
d.m.Y G:i:s
for example
additionally, your $startdate is invalid
You're probably looking for strtotime($startdate . "+ 1 day") or something
First you have to change the date format
by calling changeDateFormat("18/7/2011"): returns: 2011-07-18
if your parsing argument
function changeDateFormat($vdate){
$pos = strpos($vdate, '/');
if ($pos === false) return $vdate;
$pieces = explode("/", $vdate);
$thisday = str_pad($pieces[0], 2, "0", STR_PAD_LEFT);
$thismonth = str_pad($pieces[1], 2, "0", STR_PAD_LEFT);
$thisyear = $pieces[2];
$thisdate = "$thisyear-$thismonth-$thisday";
return $thisdate;
}
And this..
$startdate = changeDateFormat($startdate);
$enddate = date('Y-m-d', strtotime($startdate . "+".$noOfDays." day"));
This will work
$startdate = "18/7/2011";
$enddate = date('d/m/Y', strtotime($startdate) + strtotime("+1 day", 0));
echo $startdate;
echo $enddate;
First, the start date is parsed into integer, then the relative time is parsed.
You might also utilize the second parametr of strToTime:
$startdate = "18/7/2011";
$enddate = date('d/m/Y', strtotime("+1 day", strtotime($startdate)));
Related
I have a problem and I don't understand where it is :
So If I do :
$end_date = date('Y-m-d H:i:s',strtotime("+ $frequency days")); --> it works
If I do :
$end = $o_user->end;
$o_user->end = date($end, strtotime("+ $frequency days")); ---> not work
I tested and the 2 dates have the format : Y-m-d H:i:s
Where is my error ? Please help me. Thx in advance
Date's first param is the format, not an another date.
It should be something like this:
$o_user->end = date("Y-m-d H:i:s", strtotime($end . " +$frequency days"));
Maybe you just want to do
$o_user->end->modify("+ $frequency days");
It's even more readable and compact.
BTW your error is that date() function expect as first parameter a string (the date format)
Change to $o_user->end = date('Y-m-d H:i:s', strtotime($end, "+". $frequency. "days"));
You can use below code
$i_frequency = 4;
$end = '2016-05-23 10:48:42';
echo "==" . date('Y-m-d', strtotime("+$i_frequency days", strtotime($end)));
OR
$i_frequency = 4;
$end = '2016-05-23 10:48:42';
echo "==" . addDate($end, $i_frequency);
function addDate($date, $day)//add days
{
$sum = strtotime(date("Y-m-d", strtotime("$date")) . " +$day days");
$dateTo = date('Y-m-d', $sum);
return $dateTo;
}
I am generating next date using the following code:
$s1=date('d/M/Y', strtotime('+1 day'));
echo $s1;
for ex: Assume current date is 26/Aug/2014.
so above code generates 27/Aug /2014 and storing in varible $s1.
By using the varible s1 i want to create 28/Aug/2014. how to create?
I dont want to use '+2 day' in STRTOTIME function. I want to generate next day based on variable $s1.
You can do it all with strtotime() but you have to remember that strtotime assumes a USA date format when it see's a / forward slash as a seperator.
So before using $s1 you need to convert the / to a - so it assumes a sensible data format is being used.
$s1=date('d/M/Y', strtotime('+1 day'));
echo $s1.PHP_EOL;
// change date format as strtotime assumes USA dates
$date = strtotime( '+1 day', strtotime( str_replace('/','-',$s1) ) );
echo date('d/M/Y', $date);
When run on 26/Aug/2014 the result would be
27/Aug/2014
28/Aug/2014
The best way (using strtotime):
$tomorrow = strtotime('+1 day');
$twoDaysHence = strtotime('+1 day', $tomorrow);
echo date('d/M/Y', $tomorrow);
echo date('d/M/Y', $twoDaysHence);
In other words, leave your date variables in the form of UNIX timestamps as returned by strtotime until you need to display them. Because you can do calculations directly with them in this format. Once you format that to a date string, you'll have to convert them back into a malleable form first. strtotime doesn't recognise the format d/M/Y automatically, so that makes that all the harder. You should use DateTime in that case:
$tomorrow = date('d/M/Y', strtotime('+1 day'));
$timestamp = DateTime::createFromFormat('d/M/Y', $tomorrow);
$timestamp->modify('+1 day');
echo $timestamp->format('d/M/Y');
You can use something like following:
$newvariable = strtotime ('+2 day' , $s1);
this is a very simple part
$s1=date('d/M/Y', strtotime('+2 day'));
echo $s1;
and if you want then copy the value of $s1 in another variable
function date_addDate($text, $da=0, $ma=0, $ya=0, $ha=0)
{
$h=date('H',strtotime($text));
$d=date('d',strtotime($text));
$m=date('m',strtotime($text));
$y=date('Y',strtotime($text));
$fromTime =date("Y-m-d H:i:s", mktime($h+$ha, 0, 0, $m+$ma, $d+$da, $y+$ya));
return $fromTime;
}
$date = date("Y-m-d H:i:s");
// $da days
// $ma months
// $ya years
// $ha hours
echo date_addDate($date, $da=0, $ma=0, $ya=0, $ha=0);
//out put : current date
echo date_addDate($date, $da=2, $ma=0, $ya=0, $ha=0);
//out put : As you want
Try this
$s1=date('d/M/Y', strtotime('+1 day'));
echo $s1; echo "<br/>";
$date = strtotime(strtotime($s1). ' + 2 days');
$s2 = date('d/M/Y', $date);
echo $s2;
Now it's edited!! Check it!
What about using DateTime ?
$d1 = new DateTime(date('Y-m-d'));
$d1->format('d/m/Y'); // 26/08/2014
$d1->modify('+1 day');
$d1->format('d/m/Y'); // 27/08/2014
I read about this but does not working for me. Here is my code:
$today = date_create()->format("d/m/Y"); // Today is 25/04/2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime($today . " + $num_days days"));
The value that I get from $end_date is 31/12/1969. What am I doing wrong?
Try this instead:
$end_date = date("d/m/Y", strtotime("+ $num_days days", time()));
EDIT: I changed the $today variable to just time() which is essentially getting you the same information if you're just looking for today's date.
From what it looks like you're trying to do, you don't even need $today (as it defaults to now if date is not supplied), so you could just do eg:
$end_date = date("d/m/Y", strtotime("+ 5 days"));
echo $end_date;
result would be
30/04/2013
if you want to provide a date, you need the parameters the other way round, as per the manual:
strtotime ( string $time [, int $now = time() ] )
date_create() return a DateTime object.
You could use DateTime::modify method.
$date = new \DateTime(); // Defaults to Today
$num_days = 123;
$date->add(
new \DateInterval('P' . $num_days . 'D')
);
echo $date->format('d-M-Y');
$today = date_create()->format("d/m/Y"); // Today is 25/04/2013
$num_days = date_create()->format("d");
echo $end_date = date("d/m/Y", strtotime(" + $num_days days"));
<?
// note change of $today format
$today = date_create()->format("d-m-Y"); // Today is 25-04-2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime("+" . $num_days . " days", strtotime($today)));
?>
Does PHP calculate weeks as being Sunday - Saturday? For a given date I am trying to determine the beginning and ending date of it's week as well as the beginning date of the next/previous weeks. Everything works fine unless I pass in a Sunday and it thinks the date is in a previous week.
$start = $_GET['start'];
$year = date('Y', strtotime($start));
$week = date('W', strtotime($start));
$sunday = strtotime($year.'W'.$week.'0');
$next = strtotime('+7 Days', $sunday);
$prev = strtotime('-7 Days', $sunday);
echo '<p>week: ' . $week . '</p>';
echo '<p>sunday: ' . date('Y-m-d', $sunday) . '</p>';
echo '<p>next:' . date('Y-m-d', $next) . '</p>';
echo '<p>prev: ' . date('Y-m-d', $prev) . '</p>';
Outcome:
2011-01-09 (Sunday)
Week: 01
WRONG
2011-01-10 (Monday)
Week: 02
RIGHT
2011-01-15 (Saturday)
Week: 02
RIGHT
PHP doesn't think about weeks at all, if you're getting the wrong results, it's because your math is off. :)
$date = strtotime('2011-1-14');
$startingSunday = strtotime('-' . date('w', $date) . ' days', $date);
$previousSaturday = strtotime('-1 day', $startingSunday);
$nextWeekSunday = strtotime('+7 days', $startingSunday);
As Dr.Molle point out, the information about "W" is correct. Your problem is here:
$sunday = strtotime($year.'W'.$week.'0');
$sunday = strtotime($year.'W'.$week.'0');
$next = strtotime('+7 Days', $sunday);
$prev = strtotime('-7 Days', $sunday);
Then you called strtotime on a Timestamp object (sorry, I don't know the exact term).
The wrong type of parameter (timestamp and string are used not correctly) is the cause of the problem. Here's my piece of code to determine the week and the beginning day of the week:
<?php
$date = '2011/09/09';
while (date('w', strtotime($date)) != 1) {
$tmp = strtotime('-1 day', strtotime($date));
$date = date('Y-m-d', $tmp);
}
$week = date('W', strtotime($date));
echo '<p>week: ' . $week . '</p>';
?>
To fully understand, you should take a look on date & strtotime manual.
As defined in ISO_8601, what date('W') refers to, a week starts with monday.
But be careful and read about the ISO-week: http://en.wikipedia.org/wiki/ISO_week_date
Maybe the result is not always like expected.
example:
date('W',mktime(0, 0, 0, 1, 1, 2011))
It will return 52 instead of 01, because the first ISO-week of a year is the first week with at least 4 days in the given year.
As 2011-1-1 was a saturday, there are only 2 days, so 2011-1-1 is in ISO in the last week of 2010(52) and not in the first week of 2011.
The function date('W') uses the ISO-8601 definition, therefore Monday is the first day of the week.
In place of date('W') use strftime('%U').
Example:
$date = strtotime('2011-01-09');
echo strftime('%U',$date);
Outcome:
02
The code:
$date = strtotime('2012-05-06');
$sunday = date('Y-m-d', strtotime(strftime("%Y-W%U-0", $date)));
$sturday = date('Y-m-d', strtotime(strftime("%Y-W%U-6", $date)));
echo $sunday . "\n";
echo $saturday;
Outcome:
2012-05-06
2012-05-12
I have a date returned as part of a MySQL query in the form 2010-09-17.
I would like to set the variables $Date2 to $Date5 as follows:
$Date2 = $Date + 1
$Date3 = $Date + 2
etc., so that it returns 2010-09-18, 2010-09-19, etc.
I have tried
date('Y-m-d', strtotime($Date. ' + 1 day'))
but this gives me the date before $Date.
What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?
All you have to do is use days instead of day like this:
<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>
And it outputs correctly:
2010-09-18
2010-09-19
If you're using PHP 5.3, you can use a DateTime object and its add method:
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');
Take a look at the DateInterval constructor manual page to see how to construct other periods to add to your date (2 days would be 'P2D', 3 would be 'P3D', and so on).
Without PHP 5.3, you should be able to use strtotime the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):
$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
From PHP 5.2 on you can use modify with a DateTime object:
http://php.net/manual/en/datetime.modify.php
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');
Be careful when adding months... (and to a lesser extent, years)
Here is a small snippet to demonstrate the date modifications:
$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
You can also use the following format
strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));
You can stack changes this way:
strtotime("+1 day", strtotime("+1 year", strtotime($date)));
Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day and <timestamp>, you can just pass in the timestamp as the second parameter of strtotime.
Here has an easy way to solve this.
<?php
$date = "2015-11-17";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>
Output will be:
2015-11-22
Solution has found from here - How to Add Days to Date in PHP
Using a variable for Number of days
$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.' days');
echo new Date('d/m/Y', $newDate); //format new date
Here is the simplest solution to your query
$date=date_create("2013-03-15"); // or your date string
date_add($date,date_interval_create_from_date_string("40 days"));// add number of days
echo date_format($date,"Y-m-d"); //set date format of the result
This works. You can use it for days, months, seconds and reformat the date as you require
public function reformatDate($date, $difference_str, $return_format)
{
return date($return_format, strtotime($date. ' ' . $difference_str));
}
Examples
echo $this->reformatDate('2021-10-8', '+ 15 minutes', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 hour', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 day', 'Y-m-d H:i:s');
To add a certain number of days to a date, use the following function.
function add_days_to_date($date1,$number_of_days){
/*
//$date1 is a string representing a date such as '2021-04-17 14:34:05'
//$date1 =date('Y-m-d H:i:s');
// function date without a secrod argument returns the current datetime as a string in the specified format
*/
$str =' + '. $number_of_days. ' days';
$date2= date('Y-m-d H:i:s', strtotime($date1. $str));
return $date2; //$date2 is a string
}//[end function]
All have to use bellow code:
$nday = time() + ( 24 * 60 * 60);
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";
Another option is to convert your date string into a timestamp and then add the appropriate number of seconds to it.
$datetime_string = '2022-05-12 12:56:45';
$days_to_add = 1;
$new_timestamp = strtotime($datetime_string) + ($days_to_add * 60 * 60 * 24);
After which, you can use one of PHP's various date functions to turn the timestamp into a date object or format it into a human-readable string.
$new_datetime_string = date('Y-m-d H:i:s', $new_timestamp);