PHP date; How to find the next year? - php

I want to get today's date + one year. How do I achieve this with PHP's date functions?

echo date('Y', strtotime('+1 year'));

You can use strtotime and date
$date = '2010-09-16';
echo date('Y-m-d', strtotime("+12 months $date"));
// 2011-09-16
On a sidenote: DateTime questions like this have been answered over and over again, so you could have found how to add to a date easily by using the search function.

From PHP's documentation:
<?php
$date = new DateTime($your_supposed_date);
$date->add(new DateInterval('P1Y'));
echo $date->format('Y-m-d') . "\n";
?>
Gordon's much cleaner version (Thank you!):
<?php
$date = new DateTime("+12 months $theDate");
echo $date->format('Y-m-d') . "\n";
?>

$Ad_year = 2015-10-20
<?php echo $Ad_year + 1?>
Result 2016

The shortest version:
echo (int)date('Y') + 1;

You could use the new Datetime and Datetime_Intervall-classes introduced in the later PHP 5-versions.
I once posted an answer in this question. Maybe it helps you :)
The advantage is, that this classes also checks for leap-seconds and leap-years, timezones, etc.

If you're working with timestamps
echo time()+60*60*24*365

Best and easy solution...
You can change month or year or day.
date('Y-m-d',strtotime("+1 day +2months +1 year"));

Below code also return next year from current date:
<?php echo date('Y', strtotime('+12 month'));>

Related

Convert date and time to only year in php

I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');

Carbon::now() - only month

I couldn't find anywhere in documentation how to show current year or month with Carbon?
when i write this:
Carbon\Carbon::now('m');
it gives me the whole time stamp, but I just need the month
like
date('m');
but it must be Carbon!
How can I achieve this?
$now = Carbon::now();
echo $now->year;
echo $now->month;
echo $now->weekOfYear;
Update:
even this works since Laravel 5.5^
echo now()->month
I think you've already worked this out in a comment, but just for clarity: Carbon extends PHP's native DateTime class, so you can use any of the methods available on that, like format:
Carbon::now()->format('M');
(where M is the modifier for A short textual representation of a month, three letters)
You can use these both ways to get the current month
Carbon::now()->month;
or
Carbon::now()->format('m');
Just use this in your any blade file for print year:
{{ \Carbon\Carbon::now()->year }}
I wanted to get the current month and got to this question, to get the current month:
$now = Carbon::now();
$monthStart = $now->startOfMonth();
w/ Carbon + Laravel:
now()->format('M')
use Carbon\Carbon;
$today= Carbon::now();
echo $today->year;
echo $today->month;
echo $today->day;
echo $today->hour;
echo $today->minute;
echo $today->second;
Laravel 8
return date('m');
or
return now()->format('m');
You cant call it statically
use
$now = Carbon::now();
echo $now->month

Add 1 year to a date

I have a date 01/31/2014, and I need to add a year to it and make it 01/31/2015. I am using
$xdate1 = 01/31/2014;
$xpire = strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year");
But it is returning 31474800.
Waaaay too complicated. You're doing multiple date<->string conversions, when
php > $x = strtotime('01/31/2014 +1 year');
php > echo date('m/d/Y', $x);
01/31/2015
would do the trick.
Another way is below:
<?php
$date = new DateTime('2014-01-31');
$date->add(new DateInterval('P01Y'));
echo $date->getTimestamp();
?>
There are 2 mistakes here.
You are missing the quote sign " when assigning to $xdate1. It should be
$xdate1 = "01/31/2014";
And the second, to get "01/31/2015", use the date function. strtotime returns a timestamp, not a date format. Therefore, use
$xpire = date("m/d/Y", strtotime(date("m/d/Y", strtotime($xdate1)) . " +1 year"));
May I introduce a simple API extension for DateTime with PHP 5.3+:
$xdate1 = Carbon::createFromFormat('m/d/Y', '01/31/2014');
$xpire = $xdate1->addYear(1);
First make $xdate1 as string value like
$xdate1 = '01/31/2014';
then apply date function at it like bellow
$xpire = date('m/d/Y', strtotime($xdate1.' +1 year')); // 01/31/2015

PHP Date Calculation to display the next 1st June

I need to be able to calculate a date using PHP that displays the next 1st of June. So, today is 15th April 2013 therefore I need to display 01/06/2013 (UK format). If the date was 5th August 2013 I would need to display 01/06/2014.
Can anyone help?
Thanks,
John
You can achieve this using :
$now = time();
$june = strtotime("1st June");
if ($now > $june)
echo date("d/m/Y", strtotime('+1 year', $june));
else
echo date("d/m/Y", $june);
Hope this helps :)
For this you can achieve by checking the present month
if(date('m')>06)
{
$date= date('d-m-Y',strtotime("next year June 1st"));
}
else{
$date= date('d-m-Y',strtotime("this year June 1st"));
}
echo $date;
Create a new DateTime object for the current year. DateTime is the preferred way to handle dates in PHP.
If it's too early, create a new datetime object for the following year.
Finally, use 'format' to output.
$d = new DateTime(date('Y').'-08-05');
if ($d < new DateTime()) {
$d = new DateTime((date('Y')+1).'-04-15');
}
echo $d->format('d/m/Y');
You can achieve this using this tutorial. You can define time zone and display the date as per your format.
Check this manual. http://php.net/manual/en/function.date.php
clear examples are given here:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
echo $today = date("d/m/y"); // 03/10/01
?>

how can i get which month a given year day is in in php?

I have been given year day (1-366) and I need to figure out which month it is in, how can I do this?
Well, I actually have a date string like : year, day or year, minute of day, second and I ultimately want to create a POSIX timestamp from it, how can I do this?
Thank you!
If you have PHP >= 5.3, then you can use DateTime::createFromFormat.
$day = 176;
$date = DateTime::createFromFormat('z', $day);
echo $date->getTimestamp(); // 1372275280
<?php
$year=2013;
$d=360;
echo date("m",strtotime("1/1/$year + $d days"))
?>
Use the date function to get a posix time stamp.
To get the month of a certain date, use intval(date('m'), mktime($h,$m,$s,$month,$day,$year))

Categories