This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 8 years ago.
I have found several posts regarding this topic but I cannot get my function to work.
If the amount of days to add > 0, the returning date = 70-01-01
Example:
$date='12-07-11'
$days='5'
Should return 17-07-11 instead of 70-01-01
I need to use the variables because $date and $days are results from an oracle query.
function addDayswithdate($date,$days)
{
$originalDate = $date;
$newDate = date("y-m-d", strtotime($originalDate));
if ($days>0)
{
$var="+". $days ." days";
$date = strtotime($var, strtotime($newDate));
}
$originalDate = $date;
$newDate = date("y-m-d", strtotime($date));
return $newDate;
}
Use Datetime::createFromFormat and DateInterval
$date = DateTime::createFromFormat('d-m-y','12-07-11');
$date->add(new DateInterval('P5D'));
echo $date->format('d-m-Y') . "\n";
Why not keep it simpler?
function addDays($date, $days)
{
$time = strtotime($date);
$new_time = $time + (86400 * $days) //86400 seconds per day
return $new_time;
}
function addDays($date, $days)
{
$date_uts = strtotoime($date);
$add_days = $days*86400;
$final_date = date('Y-m-d', $date_uts+$add_days);
return $final_date;
}
Why not do it like this:
echo date('Y-m-d', strtotime($date + $days .'days'));
In addition of using vanilla php DateTime class, you can use Carbon if you have to work often with dates.
Carbon let you do anything like this
$date = Carbon::now()->addDays(5);
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;
}
This question already has answers here:
Adding 30 minutes to time formatted as H:i in PHP
(7 answers)
Closed 9 years ago.
I have the following code
$time = "12:00";
$duration = 90 . " minutes";
$arrival = strtotime("+" . $duration, $time);
Output should be 13:30.
I get the following error: "A non well formed numeric value encountered in" (line with $arrival)
What can I do?
The following code should work:
$time = strtotime("12:00");
$duration = "+90 minutes";
$arrival = strtotime($duration, $time);
print(date("H:i", $arrival));
Demo: https://eval.in/95328
Read more about Unix timestamp and the PHP function strtotime.
Consider using PHP DateTime and DateInterval classes, like this:
$time = new \DateTime('now');
$time->add( new \DateInterval('PT90M') );
Try This Simple code :
<?php $time = strtotime('12:00');
$more = date("H:i", strtotime('+90 minutes', $time)); ?>
I've tried to add 7 days to 2013-10-26 and got back 2013-11-01. But it have to be 2013-11-02. My old function was something like this:
public static function add($date, $years = 0, $months = 0, $days = 0)
{
$date = explode('-', $date);
return date(
'Y-m-d',
mktime(0, 0, 0, $date[1] + $months, $date[2] + $days, $date[0] + $years)
);
}
This was correct but too slow. I've made a new one that is more specialized:
public static function adddays($date, $days = 1)
{
if ($days == 0) return $date;
return date('Y-m-d', strtotime($date) + 86400 * $days);
}
It works mostly correct. Not in this case. If you let calculate strtotime('2013-10-26') % 86400 then you will find out it is 10p.m. and for some reason it makes a difference.
I'm working with version 5.3.2.
Speed test:
Repeated 1000 runs for the 3 versions
DateTime : +7 day : strtotime
26ms : 43ms : 41ms
30ms : 44ms : 42ms
25ms : 42ms : 43ms
30ms : 48ms : 49ms
So more lines and a faster result. I choose DateTime of Amal.
$date = new DateTime('2013-10-26');
$days_to_add = 7;
$date->add(new DateInterval('P' . $days_to_add . 'D'));
$date->format('Y-m-d');
Thank you. But there is still the question why it wasn't working correctly from the beginning.
The best method is to use DateTime class:
$date = new DateTime('2013-10-26');
$days_to_add = 7;
$date->add(new DateInterval('P' . $days_to_add . 'D'));
echo $date->format('Y-m-d');
Output:
2013-11-02
Demo!
You can use DateTime class to manipulate dates:
function add($date, $years = 0, $months = 0, $days = 0)
{
return date_create($date)->modify("$years year $months month $days day")->format('Y-m-d');
}
Demo
But there is still the question why it wasn't working correctly from the beginning.
Because you are using date function that is timezone and DST aware with combination with strtotime which isn't. For your example, you could use gmdate that will always return time in UTC/GMT.
Speed test different combinations, and you will see that fastest one is gmdate+strtotime combination. My run on 1M loops:
addDays_v1 needed 4.5433s
addDays_v2 needed 7.4234s
addDays_v3 needed 7.6924s
addDays_v4 needed 24.1624s
Try like
return date('Y-m-d', strtotime("+7 day",$date));
You can try like this-
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('7 days'));
echo date_format($date, 'Y-m-d');
?>
Reff: http://in1.php.net/manual/en/datetime.add.php
Changing the last line to:
return date('Y-m-d', strtotime($date . ' 00:00:00 UTC') + 86400 * $days);
And everything is working fine. I've got my 2013-11-02.
I have a variable called $effectiveDate containing the date 2012-03-26.
I am trying to add three months to this date and have been unsuccessful at it.
Here is what I have tried:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
and
$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");
What am I doing wrong? Neither piece of code worked.
Change it to this will give you the expected format:
$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.
$date = new DateTime('now');
$date->modify('+3 month'); // or you can use '-90 day' for deduct
$date = $date->format('Y-m-d h:i:s');
echo $date;
I assume by "didn't work" you mean that it's giving you a timestamp instead of the formatted date, because you were doing it correctly:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version
You need to convert the date into a readable value. You may use strftime() or date().
Try this:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;
This should work. I like using strftime better as it can be used for localization you might want to try it.
Tchoupi's answer can be made a tad less verbose by concatenating the argument for strtotime() as follows:
$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );
(This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.)
The following should work,Please Try this:
$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);
Following should work
$d = strtotime("+1 months",strtotime("2015-05-25"));
echo date("Y-m-d",$d); // This will print **2015-06-25**
Add nth Days, months and years
$n = 2;
for ($i = 0; $i <= $n; $i++){
$d = strtotime("$i days");
$x = strtotime("$i month");
$y = strtotime("$i year");
echo "Dates : ".$dates = date('d M Y', "+$d days");
echo "<br>";
echo "Months : ".$months = date('M Y', "+$x months");
echo '<br>';
echo "Years : ".$years = date('Y', "+$y years");
echo '<br>';
}
As of PHP 5.3, DateTime along with DateInterval could be a feasible option to achieve the desired result.
$months = 6;
$currentDate = new DateTime();
$newDate = $currentDate->add(new DateInterval('P'.$months.'M'));
echo $newDate->format('Y-m-d');
If you want to subtract time from a date, instead of add, use sub.
Here are more examples on how to use DateInterval:
$interval = new DateInterval('P1Y2M3DT4H5M6S');
// This creates an interval of 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds.
$interval = new DateInterval('P2W');
// This creates an interval of 2 weeks (which is equivalent to 14 days).
$interval = new DateInterval('PT1H30M');
// This creates an interval of 1 hour and 30 minutes (but no days or years, etc.).
The following should work, but you may need to change the format:
echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));
public function getCurrentDate(){
return date("Y-m-d H:i:s");
}
public function getNextDateAfterMonth($date1,$monthNumber){
return date('Y-m-d H:i:s', strtotime("+".$monthNumber." months", strtotime($date1)));
}
I've got a date in this format:
2009-01-01
How do I return the same date but 1 year earlier?
You can use strtotime:
$date = strtotime('2010-01-01 -1 year');
The strtotime function returns a unix timestamp, to get a formatted string you can use date:
echo date('Y-m-d', $date); // echoes '2009-01-01'
Use strtotime() function:
$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
Using the DateTime object...
$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');
Or using now for today
$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
an easiest way which i used and worked well
date('Y-m-d', strtotime('-1 year'));
this worked perfect.. hope this will help someone else too.. :)
On my website, to check if registering people is 18 years old, I simply used the following :
$legalAge = date('Y-m-d', strtotime('-18 year'));
After, only compare the the two dates.
Hope it could help someone.
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
Although there are many acceptable answers in response to this question, I don't see any examples of the sub method using the \Datetime object: https://www.php.net/manual/en/datetime.sub.php
So, for reference, you can also use a \DateInterval to modify a \Datetime object:
$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));
echo $date->format('Y-m-d');
Which returns:
2008-01-01
For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php
You can use the following function to subtract 1 or any years from a date.
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
And looking at above examples you can also use the following
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));