PHP - add 1 day to date format mm-dd-yyyy - php

<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);
?>
This is driving me crazy and seems so simple. I'm pretty new to PHP, but I can't figure this out. The echo returns 01-01-1970.
The $date will be coming from a POST in the format m-d-Y, I need to add one day and have it as a new variable to be used later.
Do I have to convert $date to Y-m-d, add 1 day, then convert back to m-d-Y?
Would I be better off learning how to use DateTime?

there you go
$date = "04-15-2013";
$date1 = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));
echo $tomorrow;
this will output
04-16-2013
Documentation for both function
date
strtotime

$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$date->modify('+1 day');
echo $date->format('m-d-Y');
See it in action
Or in PHP 5.4+
echo (DateTime::createFromFormat('m-d-Y', '04-15-2013'))->modify('+1 day')->format('m-d-Y');
reference
DateTime::createFromFormat()

$date = strtotime("+1 day");
echo date('m-d-y',$date);

use http://www.php.net/manual/en/datetime.add.php like
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');
output
2000-01-2

The format you've used is not recognized by strtotime(). Replace
$date = "04-15-2013";
by
$date = "04/15/2013";
Or if you want to use - then use the following line with the year in front:
$date = "2013-04-15";

Actually I wanted same alike thing,
To get one year backward date, for a given date! :-)
With the hint of above answer from #mohammad mohsenipur
I got to the following link, via his given link!
Luckily, there is a method same as date_add method, named date_sub method! :-)
I do the following to get done what I wanted!
$date = date_create('2000-01-01');
date_sub($date, date_interval_create_from_date_string('1 years'));
echo date_format($date, 'Y-m-d');
Hopes this answer will help somebody too! :-)
Good luck guys!

Related

PHP: Incorrect strtotime()

I had gone through various stackoverflow solutions and other blogs but still it doesn't fix my problem.
Let's say that the date today is: 2013-12-28 and I want to get the date after 1 month and it is supposed to display 2014-01-28.
$date = date('o-m-d');
$final = date('o-m-d', strtotime("+1 month", $date));
echo $final;
Above is my code. It returns 02/01/1970.
I have also tried the mktime method but still it displays the 1970 output.
What am I doing wrong?
BTW. I am working this on a hosted server.
Thanks ahead. :)
Use DateTime function modify
$date = new DateTime( 'o-m-d' );
echo $date->modify( '+1 month' )->format('o-m-d');
If you want the current date +1 month use:
$final = date('o-m-d', strtotime("+1 month"));
Or with a given date:
$date = date('o-m-d');
$final = date('o-m-d', strtotime($date . " +1 month"));
echo $final;
If you want to use the second parameter of strtotime it has to be a timestamp.
Go the OOP way..
<?php
$date = new DateTime('2013-12-28');
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); //prints 2014-01-28

Adding Days to a Date with PHP

I am trying to add a certain amount of days to a set date in PHP. However, all of the code I use is not working. Here is the code I am currently experiencing problems with:
echo date("2013-12-01", strtotime("+7 days"));
I wanting to add 7 days to the date above. When I echo out this code, it just prints '2013-12-01'. Is there a way to do this?
Thanks
For the sake of completeness, here's how you do it with DateTime():
$datetime = new DateTime("2013-12-01");
$datetime->add(new DateInterval('P7D'));
echo $datetime->format('Y-m-d');
or
$datetime = new DateTime("2013-12-01");
$datetime->modify('+7 days');
echo $datetime->format('Y-m-d');
You can use date_add() function:
$date = date_create('2013-12-01');
date_add($date, date_interval_create_from_date_string('7 days'));
echo date_format($date, 'Y-m-d');
This will output 2013-12-08
It must be like this:
$NewDate = date('Y-m-d', strtotime("2013-12-01" . " +7 days"));
echo $NewDate;

Calculating future date using php strtotime() not give right answer

I want to get future date after specific date
for this i use strtotime() function but it not work for me
then I use following code
$d1='2012-11-08';
$d2=date($d1, strtotime('+3 days'));
echo $d2;
output is
2012-11-08
2012-11-08
not
2012-11-11
but output is not 2012-11-11
output is 2012-11-08
i can't solve this what is error i do and how i solve this?
$d1='2012-11-08';
$d2=new DateTime($d1);
$d2->modify('+3 day');
echo $d2->format('Y-m-d');
Try using date_add
$d1 = '2012-11-08';
$d2 = date_add($d1, date_interval_create_from_date_string('3 days'));
echo $d2;
http://www.php.net/manual/en/datetime.add.php
If you're not running PHP 5.3, this should work:
$d1 = '2012-11-08';
$d2 = strtotime('+3 days', strtotime($d1));
echo date('Y-m-d', $d2);
You are using the date() function incorrectly. As per the documentation, date() takes a format string followed by an optional timestamp. You're giving it a date string and another date.
You can do what you want like this, where strtotime is used to modify the date,
$date = "2012-11-08";
echo date("Y-m-d", strtotime($date. " + 3 days"));
But if you're running PHP 5.2+ you should probably use the DateTime class, as it's got much better date handling, and it's easier to see what's going on with it.
$datetime = new DateTime("2012-11-08");
$datetime->modify("+ 3 days");
echo $datetime->format("Y-m-d");
Actually you wrongly added the strtotime(), it works when you use current date, if you want to add with customize date, You can try this,
<?php
$d1='2012-11-08';
$d2 = strtotime ( '+3 day' , strtotime ( $d1 ) ) ;
$d3 = date ( 'Y-m-d' , $d2);
?>
if you want to add date from current date, use the following,
$d1 = Date('Y-m-d', strtotime("+3 days"));
Whilst strtotime() is a handy tool, it is prone to locale issues.
I would instead use PHP's mature DateTime class, eg
$dt = new DateTime($d1);
echo $dt->add(new DateInterval('P3D'))->format('Y-m-d');

Subtracting a number of years from a Date

Say I have code: date('Y')."-".date('m')."-".date('d') How would I go about removing a number of years from the date that piece of code gives me?
$date = strtotime('-2 years');
And since your printing code is nothing but a convoluted version of this:
echo date('Y-m-d', $date);
... you could simplify everything into a one-liner:
echo date('Y-m-d', strtotime('-2 years'));
For your situation you could take advantage of PHP's implicit type casting and simply use
(date('Y')-42)."-".date('m')."-".date('d')
or shorter
(date('Y')-42)."-".date('m-d')
But, as Álvaro G. Vicario observed in a comment, there is the dreaded 29th of February that doesn't exist in all years so you'll have to use strtotime
Example:
$someDay = strtotime('2009-11-23');
$threeYearsBefore = strtotime('-3 years', $someDay);
echo date('Y-m-d', $threeYearsBefore);
or, relative to the current time,
$threeYearsBefore = strtotime('-3 years');
echo date('Y-m-d', $threeYearsBefore);
As of PHP5.2 you can use the DateTime class along with DateInterval to accomplish as well:
$now = new DateTime();
$two_years_ago = $now->sub(new DateInterval("P2Y"));
echo $two_years_ago->format('Y-m-d');
echo date("Y-m-d", strtotime('-20 years'));

Date minus 1 year?

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

Categories