Last active
October 12, 2020 21:53
-
-
Save rochow/147efab362969e7872d8 to your computer and use it in GitHub Desktop.
PHP Current & Next City
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Dates on left (nice easy format), cities on right | |
$locations = array( | |
'1st June 2015' => 'Dubai (GMT+4)', | |
'8th July 2015' => 'Budapest (GMT+2)', | |
'1st August 2015' => 'London (GMT+1)', | |
'14th August 2015' => 'Scotland (GMT+1)' | |
); | |
$today = strtotime( 'now' ); | |
$current_city = ''; | |
$next = false; | |
// Loop through locations to find current + next | |
foreach( $locations as $date => $location ) { | |
// If previous is current, then we'll set this as the next | |
if( $next ) { | |
$next_city = $location; | |
$next = false; | |
} | |
// If today greater than arrival date, set this as current (note: will be set multiple times until we end up at current) | |
if( $today >= strtotime( $date ) ) { | |
$current_city = $location; | |
$next = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment