Created
March 30, 2012 16:57
-
-
Save trovster/2252907 to your computer and use it in GitHub Desktop.
A PHP function to output start and end dates, allowing for for short syntax March 3rd-10th 2012 (if in same month & year).
This file contains hidden or 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 | |
| /** | |
| * event_output_start_end_date | |
| * @param string $start_date | |
| * @param string $end_date | |
| * @return string | |
| */ | |
| function event_output_start_end_date($start_date, $end_date) { | |
| $start_datetime = strtotime($start_date); // yyyy-mm-dd | |
| $end_datetime = strtotime($end_date); // yyyy-mm-dd | |
| $output = ''; | |
| if($start_datetime === $end_datetime) { | |
| // same day - 1st April 2012 | |
| $output = date('jS F Y', $start_datetime); | |
| } | |
| elseif(date('Y-m', $start_datetime) === date('Y-m', $end_datetime)) { | |
| // same year and month - 3rd - 21st March 2012 | |
| $output = sprintf('%s - %s %s', date('jS', $start_datetime), date('jS', $end_datetime), date('F Y', $start_datetime)); | |
| } | |
| elseif(date('Y', $start_datetime) === date('Y', $end_datetime)) { | |
| // same year - 29th January - 2nd February 2012 | |
| $output = sprintf('%s - %s %s', date('jS F', $start_datetime), date('jS F', $end_datetime), date('Y', $start_datetime)); | |
| } | |
| else { | |
| // completely different - 8th December 2012 - 2nd Janurary 2013 | |
| $output = sprintf('%s - %s', date('jS F Y', $start_datetime), date('jS F Y', $end_datetime)); | |
| } | |
| return $output; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bit late, but hopefully useful.