简体   繁体   中英

last year, this year, next year with php DateTime

I am trying to create a dropbox that will display the last year, the current year and the next year using the php DateTime object.

In my current code I create three objects and have to call a method on 2 of them. This seems a bit heavy on the resources.

$today = new DateTime();
$last_year=new DateTime();
$last_year->sub(new DateInterval('P1Y'));
$next_year = new DateTime();
$next_year->add(new DateInterval('P1Y'));
echo date_format($last_year, 'Y').' '.date_format($today, 'Y').' '.date_format($next_year, 'Y');

another way I found to only use 1 object is

$today = new DateTime();
echo date_format($today->sub(new DateInterval('P1Y')), 'Y').' '.date_format($today->add(new DateInterval('P1Y')), 'Y').' '.date_format($today->add(new DateInterval('P1Y')), 'Y');

but that will become very confusing. Can someone tell me a better way to do this using DateTime() ? As I will need something similar for months ?

Depending upon your version of PHP (>= 5.4), you could tidy it up a bit like this:-

$today = new DateTime();
$last_year=(new DateTime())->sub(new DateInterval('P1Y'));
$next_year = (new DateTime())->add(new DateInterval('P1Y'));
echo $last_year->format('Y').' '.$today->format('Y').' '.$next_year->format('Y');

See it working .

A more readable and concise option may be to use \\DateTimeImmutable .

$today = new DateTimeImmutable();
$one_year = new DateInterval('P1Y');

$last_year = $today->sub($one_year);
$next_year = $today->add($one_year);
echo $last_year->format('Y').' '.$today->format('Y').' '.$next_year->format('Y');

See it working .

Other than that, this all looks fine. Worry about optimisation when it is needed.

Try this. This quite efficient.

 echo date("Y");   
 echo date("Y",strtotime("-1 year"));
echo date("Y",strtotime("+1 year"))

May be you can also limit the call of new DateInterval('P1Y') by creating one object and using it for all three calculations?

$interval = new DateInterval('P1Y');

$dateTime = new DateTime();
$lastYear = $dateTime->sub($interval)->format('Y');

$dateTime = new DateTime();
$nextYear = $dateTime->add($interval)->format('Y');

$dateTime = new DateTime();
$thisYear = $dateTime->format('Y');

echo $lastYear . ' ' . $thisYear . ' ' . $nextYear;

and by breaking the single string into multiple commands always helps me in reducing confusions.

<?php

$d = new DateTime('now');
$cy = $d->format('Y'); 

// Get previous year 
$d->modify('-1 year');
$py = $d->format('Y');

//Next year : Since object has previous year, so +2 to get next year
$d->modify('+2 year');
$ny = $d->format('Y');

echo "Previous Year: ".$py."<br>";
echo "Current Year : ".$cy."<br>";
echo "Next Year : ".$ny."<br>";

$d = new DateTime('now');
$cm = $d->format('m'); 

$d->modify('-1 month');
$pm = $d->format('m'); 

$d->modify('+2 month');
$nm = $d->format('m');

echo "Previous Month: ".$pm."<br>";
echo "Current Month : ".$cm."<br>";
echo "Next Month : ".$nm."<br>";
?>

Output

Previous Year: 2013
Current Year : 2014
Next Year : 2015
Previous Month: 12
Current Month : 01
Next Month : 02

Just use basic math:

$current = date('Y');
$prev = $current - 1;
$next = $current + 1;

after reading all the answers I came up with this function to create a dropdown box for year. $name is the name of the $year_number is the number of years you want to display this function starts with one year before the current year but you can easily modify it to start displaying from earlier years.

function drop_down_box_date_year(&$dbconn, $name, $year_number){
$today = new DateTime();
$interval = new DateInterval('P1Y');
$startyear = (new DateTime())->sub($interval);
echo '<select name="'.$name.'_year">';
for($i=0;$i<$year_number;$i++){ 
    if($startyear->format('Y')==$today->format('Y')){
        echo '<option value="'.$startyear->format('Y').'" selected>'.$startyear->format('Y').'</option>';           
    }else{
        echo '<option value="'.$startyear->format('Y').'">'.$startyear->format('Y').'</option>';
    }
    $startyear->add($interval);
}
echo'</select>';

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM