Jump to content

Multi Date Range Format


brianlaughlan

Recommended Posts

Hello Everyone,

 

This is my first post. I would really love your input on this. I need a function that will allow me to take two dates and turn it into a date range. It must understand if the range splits into seperate months or even years. and there must be a "-" as a seperator.

 

Examples...

 

Range within same month:

"15 July 2012" as a start date and "17 July 2012" as an end date then the output will be "15. - 17 July 2012" (DAY. - DAY. MONTH YEAR)

 

Range spills over 2 Months:

"30 July 2012" as a start date and "1st August 2012" as an end date then the output will be "30. July - 1. August 2012" (DAY. MONTH - DAY. MONTH YEAR)

 

Range spills over 2 Years:

"30 December 2012" as a start date and "1st January 2013" as an end date then the output will be "30. December 2012 - 1. January 2013" (DAY. MONTH YEAR - DAY. MONTH YEAR)

 

I guess I need to run it through some sort of validation first to figure out what range type from above to use and then do the output. I am still pretty basic at PHP so I would love to just leave this open and see what feeback you guys can offer.

 

Thanks in advance,

 

Brian.

Link to comment
Share on other sites

I am using wordpress and I have created two custom fields on a custom post type "Course" - where the user can select a start date and an end date when creating a post or in this case "course".

 

My idea was to create a WordPress Shortcode that allows me to input these two variables and it will call the function and output the combined string. Before I create the shortcode though, I need to figure out the function and get it working on its own.

 

Any advice on the structure of such a function, or any "things to think of" would be much appreciated.

 

- Brian

 

 

Link to comment
Share on other sites

OK, so as I said, I am just a beginner, the best way I could figure out how to do this was to use IF statements and work my way backwards starting with an initail check to see if it is the exact same date. If it wasnt I then check the year, if its the same year I then check the month. If its not the same year I output the full set of dates.

 

Any advice on how I could tidy this up and make less code would be great? I added some HTML in to show messages on what is happening.

 

Any ideas?

 

 

 

<?php

$startDate = new DateTime();
$endDate = new DateTime();

$startDate->setDate(2012, 03, 25);
$endDate->setDate(2012, 03, 26);

if ( $startDate == $endDate ) {
    $message = "Exactly the same Date";
    $joinDates = $startDate->format('j').". ".$endDate->format('F')." ".$endDate->format('Y');
} else {
    if ( $startDate->format('Y') == $endDate->format('Y')) {
        if ( $startDate->format('F') == $endDate->format('F')) {
            $message = "Not same day, Same year and month";
            $joinDates = $startDate->format('j').". - ".$endDate->format('j').". ".$endDate->format('F')." ".$endDate->format('Y');
        } else {
            $message = "Not same day, Same Year NOT Month";
            $joinDates = $startDate->format('j').". ".$startDate->format('M')." - ".$endDate->format('j').". ".$endDate->format('M')." ".$endDate->format('Y');
        }
    } else {
      $message = "Not same day, NOT Same Year OR same month";
      $joinDates = $startDate->format('j').". ".$startDate->format('M')." ".$startDate->format('Y')." - ".$endDate->format('j').". ".$endDate->format('M')." ".$endDate->format('Y');

}
}

//Some HTML below to show the dates...
?>


<br />
<h1><?php  echo $joinDates;   ?></h1>
<h3><?php  echo $message;   ?></h3><br />

Start Date: <b><?php  echo $startDate->format('j F Y');   ?></b><br />
The start month is <b><?php  echo $startDate->format('F');   ?></b><br />
The start year is <b><?php  echo $startDate->format('Y');   ?></b><br />
<br />
End Date:   <b><?php  echo $endDate->format('j F Y');   ?></b><br />
The end month is <b><?php  echo $endDate->format('F');   ?></b><br />
The end year is <b><?php  echo $endDate->format('Y');   ?></b><br />

Link to comment
Share on other sites


function date_range($s_date,$e_date){
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";
}
}

$s_date = "15 July 2012";
$e_date = "17 July 2012";
echo date_range($s_date,$e_date);
echo "<br />";

$s_date = "30 July 2012";
$e_date = "1 August 2012";
echo date_range($s_date,$e_date);
echo "<br />";

$s_date = "30 December 2012";
$e_date = "1 January 2013";
echo date_range($s_date,$e_date);

Link to comment
Share on other sites

OK so.. I have got as far as below.

 

I am using this as my shortcode in the post: [daterange start=15 July 2012" end="17 July 2013]

 

but I am only getting the dots and dashes outputted  >>>>  . - .

 

It must be something stupid I am missing? or... I have completly done it the wrong way?

 

function date_range($params = array()){

    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");

    extract(shortcode_atts(array(
        's_date' => 'start',
        'e_date' => 'end',
    ), $params));

    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');

Link to comment
Share on other sites

oops... I only had to pass in some default date paramaters and use the same names in the short and now it works. Thanks for your help PFMaBiSmAd, much appreciated.

 

Use this shortcode in WP...  [daterange s_date=15 July 2012" e_date="17 July 2013]

 

function date_range($params = array()){

    extract(shortcode_atts(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');

Link to comment
Share on other sites

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.