Hello & Tidy Date Range Function :)

Hi Everyone,

I am new to WPMUDEV and just wanted to introduce myself :slight_smile: I think this community platform is outstanding and I cannot wait to jump in to share thoughts and help others out where I can. I am by no means an expert but I really want to expand my knowledge and this seems like a great place to do that! The points system is simply... genius.

ok, so as I said I am not even close to being as smart as most people on here but I was having a bit of trouble trying to take 2 separate date values and combine them into one string that made sense! After much deliberation I managed to squeeze out the function below. I am very proud of it hahaha - You will all look at it and laugh probably haha

An example could be when using a custom post type "Course" with custom fields: "Start Date" & "End Date" - What happens if the dates span over a few months of even Years. I wanted it to output without repeating values.
Day Output:
Full String... "22 Nov 2012 - 23 Nov 2012" Clean String... "22 - 23 Nov 2012"
Month Output:
Full String... "22 Nov 2012 - 3 Dec 2012" Clean String... "22 Nov - 3 Dec 2012"
Year Output:
Full String... "22nd Nov 2012 - 22nd Jan 2013" << Stays the same

I created the function to check the two dates and output one of three possible scenarios (I also turned the function into a wp shortcode to obtain the 2 values from users)
[daterange s_date="11 Nov 2012" e_date="10 Dec 2012"]

Look forward to getting to know you all. Please let me know if I could have done the date thing any better :slight_smile:



function date_range($params = array()){

's_date' => '1 January 2012',
'e_date' => '2 January 2012',
), $params));

list($s_day,$s_month,$s_year) = sscanf($s_date, "%d %s %d");
list($e_day,$e_month,$e_year) = sscanf($e_date, "%d %s %d");

if($s_year == $e_year){
// same year, either 1st or 2nd form
if($s_month == $e_month){
// same year, same month, 1st form - DAY. - DAY. MONTH YEAR
return "$s_day. - $e_day. $e_month $e_year";
} else {
// same year, different month, 2nd form - DAY. MONTH - DAY. MONTH YEAR
return "$s_day. $s_month - $e_day. $e_month $e_year";
} else {
// different year - 3rd form - DAY. MONTH YEAR - DAY. MONTH YEAR
return "$s_day. $s_month $s_year - $e_day. $e_month $e_year";
add_shortcode('daterange', 'date_range');