I am trying to add a user defined amount of months to the date previously added. $CustDate is already in YYYY-MM-DD format from previous form.
$CustDate=$_POST['formYear'] . "-" . $_POST['formMonth'] . "-" . $_POST['formDay'];
$months=$_POST['formMonthsAdded'];
$d=strtotime("+" . $months . " Months");
$CustAddedDate=date("Y-m-d", strtotime($CustDate, $d));
If I enter the date as: 2016-08-04 as the $CustDate, it gives me the same value for the $CustAddedDate.
Where am I screwing this up? Thanks!
You add the + months along with the $CustDate. Provide the $Cusdate as the second argument in the addition.
$CustAddedDate = date('Y-m-d', strtotime("+" . $months . " Months", strtotime($CustDate)));
// ^ add this with the addition
Or the DateTime variant:
$date = new DateTime($CustDate);
$date->modify('+ ' . $months . ' Month');
$CustAddedDate = $date->format('Y-m-d');
echo $CustAddedDate;
Note:
$d = strtotime('2016-03-02'); // March 2nd, 2016 -> 1456898400
echo date('Y-m-d', strtotime('+1 day', $d)); -> 2016-03-03
echo date('Y-m-d', strtotime('2010-01-02', $d)); -> 2010-01-02
The second argument for strtotime() set a time basis for any "relative" time values, like +1 day or yesterday. Since you're passing in an absolute date, 2016-08-04, there's no "relative" measure to base anything on, and your absolute date is used in its entirety for the conversion.
If you want to adjust that absolute date, you have to do something like
echo date('Y-m-d', strtotime('2016-08-04 + 1 day')) -> 2016-08-05
e.g. embed the date math into the string you're passing into strtotime, and not in the second argument.
$CustDate=$_POST['formYear'] . "-" . $_POST['formMonth'] . "-" . $_POST['formDay'];
$months=$_POST['formMonthsAdded'];
$d="+" . $months . " Months"; //not strtotime time here!
$CustAddedDate=date("Y-m-d", strtotime($d,strtotime($CustDate)));//watch the order of arguments and missing strtotime of the existing date
Related
I have the following code that is returning an unexpected answer. Please let me know what's wrong.
$start_date = new DateTime('31-03-2019');
$end_date = new DateTime('01-05-2019');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";
It is returning the following output:
day: 0 month: 1
I expect the output to be:
day: 1 month: 1
This will give you one day and one month https://3v4l.org/q0T8r
$start_date = new DateTime('31-03-2019 00:00:00');
$end_date = new DateTime('01-05-2019 24:00:00');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";
When you add 1 month to 2019-03-31, PHP will internally just increment the month value 03 to 04. The result is 2019-04-31.
As April has only 30 days, 2019-04-31 has the same meaning as 2019-05-01 has. And that's the reason, why you get one month and zero days as the result.
The DateInterval class has another handy property: days instead of m and d. It will contain the total number of days between the two dates, which equals to 31 (you have to add 31 days to 2019-03-31 to get to the 2019-05-01.
On this value you can implement your own logic, what "one month" is. If you define it as "one month = 30 days", this could be your whished result:
$start_date = new DateTime('31-03-2019');
$end_date = new DateTime('01-05-2019');
$diff = $start_date->diff($end_date);
$months = floor($diff->days / 30);
$days = $diff->days % 30;
echo "day: " . $days . " month: " . $months . "\n";
I have a variable which is holding the output of a datepicker input tag. I want to add 24 hours to it.
As it stands, if I var_dump the variable it is:
string(10) "dd/mm/yyyy"
How would I go about adding 1 day to it?
I have tried:
$finishdate2 = date('d-m-Y',strtotime($finishdate . "+1 days"));
but I seem to get some weird dates.
$date="10/05/2015";
$date1=str_replace("/","-",$date);
$tomorrow=date('d/m/Y',strtotime($date1 . "+1 days"));
You can try like this
this will help you..
<?php
$date = '2015-02-28';
$date = date('Y-m-d', strtotime($date . ' + 1 day'));
echo 'date after adding 1 day: ' . $date;
?>
You can try with it
$your_date_variable= str_replace('/', '-', $user_iput);
echo date('Y-m-d', strtotime($your_date_variable. ' + 1 days'));//2015-05-14
or
echo date('Y-m-d H:i:s', strtotime($your_date_variable. ' + 1 days'));//2015-05-14 11:07:07
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
adding one day to a date
I'm trying to add a day to a value pulled from a mysql row.
so the value getting returned is let's say
2012-10-22 22:12:13
and I want to make it
2012-11-22 22:12:13
and store it in the variable without having to interval it back into mysql and then pull it right back out.
i tried doing
$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;
with $end_date being the time logged, but it replaces the time with zeros.
am I going about this wrong?
Any help much appreciated, thank you.
This is what you want, i guess...
$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));
echo date("Y-m-d H:i:s", $date_old);
You can make use of strtotime to add the one month period:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));
Output (Demo):
2012-11-22 22:12:13
You can also make use of PHP's DateTime type and add a DateInterval of one day:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);
Output (Demo):
2012-11-22 22:12:13
The code above is PHP 5.4, in PHP 5.3 you can do:
echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
Date adding
$date = date("Y-m-d"); // current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
This could also be done as part of the MYSQL query
SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'
I'm trying to find the nth day of the month in a timestamp like so:
$day = 15;
$date = new DateTime('#' . $timestamp);
$date->modify($day . ' day of current month');
This generates an error:
Warning: DateTime::modify(): Failed to parse time string (15 day of current month) at position 7
I've also tried "day 15 of current month" and that does not work.
How can I modify my "modify" to find the nth day of the current month in the timestamp?
First retrieve the first day of the month, then add the number of days you want.
$date = new DateTime('first day of ' . date("Y-m-d", $timestamp));
$date->modify('+' . ($day-1) . 'days');
You can use php's date() formatting capabilities with a hard-set "day" value (adapt as necessary):
<?php
$timestamp = time();
$format = date('Y-15-M', $timestamp);
$fifteenth = strtotime($format);
echo "15th was a " . date("D", $fifteenth) . "\n";
Given that I have $num_years, how can I add it to Time() to get a time stamp $num_years hence?
strtotime("+$num_years years") will give you a timestamp $num_years years in the future.
try
echo strtotime("now + $num_years years");
Is this what you're after?
$num_years = 5;
$future = strtotime("+{$num_years} years");
echo strftime('%Y %a %d %m, %T', strtotime('+' . $num_years . ' years') );
Change strfttime format according your needs.
Check out strtotime man page http://php.net/manual/en/function.strtotime.php
Well if you looking for the timestamp for just that time then use mktime
mktime(0,0,0,0,0,$noOfYears);
http://www.php.net/manual/en/function.mktime.php
is this what you want ?
//Example to add 1 year to a date object
$num_years = 1;
$currentDate = date("Y-m-d");// current date
//Display the current date
echo "Current Date: ".$currentDate."<br>";
//Add one year to current date
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +$num_years years");
echo "Date After adding one year: ".date('l dS \o\f F Y', $dateOneYearAdded)."<br>";