PHP not adding days in date - php

PHP:
$date = str_replace('/', '-', $this->input->post('Insert_date'));
$data['Insert_date'] = date('Y-m-d', strtotime($date));
$data['Credit_limit'] = date($data['Insert_date'], strtotime("+10 days"));
echo $data['Insert_date'].'<br>';
echo $data['Credit_limit'].'<br>';
Output:
2017-09-01
2017-09-01
Expected Output:
2017-09-01
2017-09-11
Anyone can please help me why $data['Credit_limit'] != 2017-09-11. Why 10 days is not added in $data['Credit_limit'] How can I resolve this issue? please help me.

Your format for strtotime is wrong:
$data['Credit_limit'] = date('Y-m-d', strtotime($data['Insert_date'] . " +10 days"));
Explanation:
You need to add the date inside strtotime function. date function holds the format as the first parameter like this: date($format) .

You are using date function in wrong way
$date = str_replace('/', '-', '2017-09-01');
$data['Insert_date'] = date('Y-m-d', strtotime($date));
$data['Credit_limit'] = date("Y-m-d", strtotime("+10 days",strtotime($data['Insert_date'])));
echo $data['Insert_date'].'<br>';
echo $data['Credit_limit'].'<br>';
DEMO

The second date formatting is incorrect. Try to concatenare the date and plus expression
$date = str_replace('/', '-', $this->input->post('Insert_date'));
$data['Insert_date'] = date('Y-m-d', strtotime($date));
$data['Credit_limit'] = date('Y-m-d', strtotime($date . " + 10 days"));
echo $data['Insert_date'].'<br>';
echo $data['Credit_limit'].'<br>';

Try this:
$date = str_replace('/', '-', $this->input->post('Insert_date'));
$data['Insert_date'] = date('Y-m-d', strtotime($date));
$data['Credit_limit'] = date('Y-m-d', strtotime($data['Insert_date'] . " +10 days"));
echo $data['Insert_date'].'<br>';
echo $data['Credit_limit'].'<br>';

Related

Add number of days of an existing days

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;
}

Codeigniter add number to date

I have a number and I want to increase the month of date so if the date is 2016-01-01 will return 2016-04-01
$month = 3;
$date = date('Y-m-d', strtotime(str_replace('/', '-', $this->input->post('date'))));
Untested, But you could do something like:
$month = 3;
$newdate = date('Y-m-d', strtotime("+".$month." months", str_replace('/', '-', $this->input->post('date'))));
You can add three months in a given date by using this:
$Date = date('Y-m-d', strtotime("+3 months", strtotime(str_replace('/','-', $this->input->post('date') ))));

strftime adding +1 day also removes 5 minutes

Somehow, using strtotime and adding "+1 day" not only adds the day, but also removes 5 minutes.
In the following example I expect '2013-10-02 08:15:00', but get '2013-10-02 08:10:00':
$myDate = '2013-10-01 08:15:00';
$newDate = strtotime($myDate . ' +1 day');
$newDate = strftime("%Y-%m-%d %H:%m:00", $newDate);
debug($newDate);
//'2013-10-02 08:10:00'
BUT - if I use date() instead of strftime(), it works fine
$myDate = '2013-10-01 08:15:00';
$newDate = strtotime($myDate . ' +1 day');
$newDate = date("Y-m-d H:i:s", $newDate);
debug($newDate);
//'2013-10-02 08:15:00'
Needed a capital M instead of m.
Check http://php.net/manual/en/function.strftime.php
$myDate = '2013-10-01 08:15:00';
$newDate = strtotime($myDate . ' +1 day');
$newDate = strftime("%Y-%m-%d %H:%M:00", $newDate);
debug($newDate);

PHP add days on to a specific date?

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)));

Simplest way to increment a date in PHP?

Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.
A clean way is to use strtotime()
$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);
Will give you the 2007-03-01
It's cleaner and simpler to add 86400. :)
The high-tech way is to do:
$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');
but that's really only remotely worthwhile if you're doing, say, a sequence of transformations on the date, rather than just finding tomorrow.
You can do the addition right inside strtotime, e.g.
$today="2007-02-28";
$nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));
Another way is to use function mktime(). It is very useful function...
$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));
but I think strtotime() is better in that situation...
The simplest way...
echo date('Y-m-d',strtotime("+1 day")); //from today
OR from specified date...
echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));
Hello you can try this below especially if you are french
$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));
to translate in french you can use the setlocale() function or the function below :
function fr_date($date){
$date = explode(' ', $date);
$date = str_replace('Monday','Lundi',$date);
$date = str_replace('Tuesday','Mardi',$date);
$date = str_replace('Wednesday','Mercredi',$date);
$date = str_replace('Thursday','Jeudi',$date);
$date = str_replace('Friday','Vendredi',$date);
$date = str_replace('Saturday','Samedi',$date);
$date = str_replace('Sunday','Dimanche',$date);
$date = str_replace('January','Janvier',$date);
$date = str_replace('February','Février',$date);
$date = str_replace('March','Mars',$date);
$date = str_replace('April','Avril',$date);
$date = str_replace('May','Mai',$date);
$date = str_replace('June','Juin',$date);
$date = str_replace('July','Juillet',$date);
$date = str_replace('August','Août',$date);
$date = str_replace('September','Septembre',$date);
$date = str_replace('October','Octobre',$date);
$date = str_replace('November','Novembre',$date);
$date = str_replace('December','Décembre',$date);
$date = implode(' ',$date);
return $date;
}
$your_date = strtotime("1month", strtotime(date("Y-m-d")));
$new_date = date("Y-m-d", $your_date++);
use strtotime() With date Formate
echo date('Y-m-d', strtotime('2007-02-28' . ' +1 day'));
$early_start_date = date2sql($_POST['early_leave_date']);
$date = new DateTime($early_start_date);
$date->modify('+1 day');
$date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
$date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);
$interval = date_diff($date_a, $date_b);

Categories