4

I have a date string formatted as March 8 - 10 where the year is not provided, but based on the current day of the calendar year, this would be dates in March of the next year.

What is best approach to provide the accurate year when a date given similar to the above is past Dec 31?

Thinking something like below using $sdate > $now however this would add +1 year to any date greater than now and not consider Dec 31 as the end of current year.

$dates = trim('March 8 - 10');
$now = date("Y-m-d",strtotime("now"));

    if (strpos($dates,'-') !== false) {
            $sdate = trim(substr($dates, 0, strpos($dates, '-')));

            if ($sdate > $now) {
                $sdate = strtotime("+1 year", strtotime($sdate));
                $sdate = date("Y-m-d", $sdate);
            }

            $month = substr($sdate, 0, strpos($sdate, ' '));
            $edate = $month.substr($dates, -2, strpos($dates, '-'));
            $edate = date("Y-m-d",strtotime($edate));
        }
RonnieT
  • 2,193
  • 4
  • 32
  • 43
  • Interesting question, I'll work a bit on it and post my answer. – Pedro Lobito Oct 13 '15 at 03:17
  • In this case you're just using `March 8`, right ? – Pedro Lobito Oct 13 '15 at 03:20
  • March 8 would be the reference point for the start date. So if we took today, I would like to show March 8 2016. Or 2016-03-08 The assumption being that March 8 is occurring in 2016 since today is Oct – RonnieT Oct 13 '15 at 03:22
  • I just don't know how to reference Dec 31 being the end of 2015 so if a date is past this point it should show 2016 as the year – RonnieT Oct 13 '15 at 03:24
  • Can we assume that, if the provided date if older than the current date, is next year ? – Pedro Lobito Oct 13 '15 at 04:00
  • Not entirely. For example, if the date provided is Nov 4, that is technically past the current date of today, but not in 2016. This is where I keep running into the problem. – RonnieT Oct 13 '15 at 04:10
  • "**Nov 4, that is technically past the current date of today**" I'm not following you. – Pedro Lobito Oct 13 '15 at 04:24

3 Answers3

5

I think you're looking for something like this:

Example:

$in = trim('March 8 - 10');

$now = new DateTimeImmutable(); // Defaults to now
$start = DateTimeImmutable::createFromFormat('F j+', $in); // Parse month and day, ignore the rest

if ($now > $start) {
    $start = $start->modify("next year");
}

$end = $start->setDate(
    $start->format('Y'),               // $start year
    $start->format('n'),               // $start month
    substr($in, strrpos($in, ' ') + 1) // trailing bit of $in for day
);

echo $start->format("Y-m-d"), "\n";
echo $end->format("Y-m-d");

Output

2016-03-08
2016-03-10

Given a string like 'November 8 - 10' it'll output:

2015-11-08
2015-11-10
user3942918
  • 25,539
  • 11
  • 55
  • 67
  • Just realized DateTimeImmutable is not valid in PHP 5.3 - would I use DateTime for use in 5.3? Doing so doesnt work as you have above. What would you suggest? This solution works perfectly in 5.6 – RonnieT Oct 14 '15 at 02:11
  • Thanks Paul - I will look into upgrading PHP in general and will use your edits here. Really appreciate assistance. – RonnieT Oct 14 '15 at 02:52
2
<?php
$input = 'December 8 - 10';
//$input = 'August 8 - 10'; //un-comment to test past date (next year)
$inputFormated = DateTime::createFromFormat( 'F j+', $input.date("Y") );
$now = new DateTime( 'NOW' );
if( $now > $inputFormated ){
   $inputFormated->modify( '+1 Year' );
   echo $inputFormated->format( 'Y-m-d' );
}else{
   echo  $inputFormated->format( 'Y-m-d' );
}

December 8 - 10 -> 2015-12-08

August 8 - 10 -> 2016-08-08


DEMO:

https://eval.in/449346

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
1

An naive solution perhaps

$reference = time ();
$ts = strtotime ('03-08');
if ($ts < $reference)
  $ts += strtotime ('+1 year') - $reference;
$result = date ('Y-m-d', $ts);