I'm trying to get a date that is one year from the date I specify. Show My code looks like this:
It's returning the wrong date. Any ideas why?
abatishchev 96.2k82 gold badges294 silver badges428 bronze badges asked Dec 15, 2009 at 3:48
4
$futureDate is one year from now!
$futureDate is one year from $startDate! answered Jan 25, 2013 at 15:17
MishoMisho 2,8132 gold badges13 silver badges9 bronze badges 1 To add one year to todays date use the following:
For the other examples you must initialize $StartingDate with a timestamp value for example:
Try this
or
abatishchev 96.2k82 gold badges294 silver badges428 bronze badges answered Dec 15, 2009 at 9:09
Nidhin BabyNidhin Baby 1,5784 gold badges13 silver badges16 bronze badges 9
hope this simpler bit of code helps someone in future :) answered Jul 31, 2017 at 15:26
DeveloperDeveloper 3,6774 gold badges36 silver badges45 bronze badges Try: answered Dec 15, 2009 at 3:52
K PrimeK Prime 5,7491 gold badge24 silver badges19 bronze badges 4 just had the same problem, however this was the simplest solution:
answered Mar 29, 2013 at 9:18
GardeneeGardenee 1152 silver badges7 bronze badges 1
answered Jan 6, 2016 at 23:59
I prefer the OO approach:
Use If you just want from todays date then you can always do:
answered Dec 14, 2016 at 17:06
Andrew AtkinsonAndrew Atkinson 3,9735 gold badges41 silver badges48 bronze badges If you are using PHP 5.3, it is because you need to set the default time zone:
answered Dec 15, 2009 at 3:55
SeanJASeanJA 9,9985 gold badges31 silver badges42 bronze badges
From the docs:
answered Dec 15, 2009 at 4:00
Frank FarmerFrank Farmer 37.2k12 gold badges70 silver badges89 bronze badges 1 Try This
answered Dec 15, 2009 at 3:56
TrebyTreby 1,3206 gold badges18 silver badges26 bronze badges There is also a simpler and less sophisticated solution:
answered Jun 16, 2013 at 4:59
Daniel LimaDaniel Lima 7888 silver badges13 bronze badges My solution is: You can make it more "readable" with defines:
answered May 22, 2020 at 13:41
You can use strtotime() to get future time.
answered Oct 28, 2021 at 11:05
infomasudinfomasud 1,24311 silver badges12 bronze badges In my case (i want to add 3 years to current date) the solution was:
To Gardenee, Treby and Daniel Lima: what will happen with 29th February? Sometimes February has only 28 days :)
Tom Fuller 5,0616 gold badges32 silver badges42 bronze badges answered Nov 4, 2014 at 13:48
aiuenaiuen 332 bronze badges How to add 1 year in php date?Example 1: PHP Add Year to Date. index.php. $date = "2022-02-01"; $newDate = date('Y-m-d', strtotime($date. ' + 5 years')); ... . Output: 2027-02-01.. index.php. $newDate = date('Y-m-d', strtotime(' + 5 years')); echo $newDate; ?>. Output: Read Also: How to Add Months to Date in PHP? 2026-03-01. i hope it can help you.... How do I add 1 day to a date?const date = new Date('2022-02-21'); const dateCopy = new Date(date. getTime()); dateCopy. setDate(dateCopy. getDate() + 1); // 👇️ Tue Feb 22 2022 console.
What does date () do in PHP?The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways.
How can I change the date format in PHP?To convert the date-time format PHP provides strtotime() and date() function.. <? php.. $orgDate = "2019-02-26";. $newDate = date("m-d-Y", strtotime($orgDate));. echo "New date format is: ". $newDate. " (MM-DD-YYYY)";. |