Jump to content

Auto generate year session


Go to solution Solved by Barand,

Recommended Posts

Hi all

I am trying to create something like 2023-2024 in a loop so that each year it generate the session for me. I don't want to manually add it. What I did did not work (though I know it won't work but just did it anyway)

$cur_yr = date('Y');
$nxt_yr = date('Y', strtotime('+1 year,);
$cur_session = $cur_yr."-".$nxt_yr;

$strt_session = "2020-2021";

for($i = $strt_session; $i<= $cur_session; $i++){
    echo $i;                                 
 }

Mine won't work. But how can I get it done. 

Thanks in anticipation

Edited by I-AM-OBODO
Link to comment
https://forums.phpfreaks.com/topic/317148-auto-generate-year-session/
Share on other sites

  • Solution

Try this. You don't need date arithmetic for years.

$start = 2020;
$end   = 2029;

for ($y=$start; $y<=$end; $y++) {
    $session = sprintf('%d-%d', $y, $y+1);
    echo $session . '<br>';
}

output

2020-2021
2021-2022
2022-2023
2023-2024
2024-2025
2025-2026
2026-2027
2027-2028
2028-2029
2029-2030

 

  • Thanks 1
21 minutes ago, Barand said:

Try this. You don't need date arithmetic for years.

$start = 2020;
$end   = 2029;

for ($y=$start; $y<=$end; $y++) {
    $session = sprintf('%d-%d', $y, $y+1);
    echo $session . '<br>';
}

output

2020-2021
2021-2022
2022-2023
2023-2024
2024-2025
2025-2026
2026-2027
2027-2028
2028-2029
2029-2030

 

How can it be read from bottom, that is 2027-2028 be at the top? What is order by alternative in php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.