2

I have two queries, both related to dates.

1) I have dates in these formats, which I'm looking to normalise into the same format before saving into a database:

  • Saturday 26 July
  • Monday 28 - Wednesday 30 July
  • July 24th, 2014
  • Thu 4 Sep
  • Thu 28 Aug — Fri 19 Sep
  • 24-07-2014

Single days are quite easy to work out using strtotime(), but ranges of dates are a bit more tricky.

This, for example, doesn't work:

$dateString = "Monday 28 - Wednesday 30 July";

if (strpos($dateString, "-")) {
    $datePieces = explode("-", $dateString);
    $startDate = strtotime($datePieces[0]);
    $endDate = strtotime($datePieces[1]);
} else {
    $startDate = strtotime($dateString);
    $endDate = strtotime($dateString);
}

echo '<pre>';
echo date('d F Y', $startDate);
echo '<br/>';
echo date('d F Y', $endDate);

Because the month is only on one side of the explode(), doing it this way returns:

01 January 1970
30 July 2014

2) I need a way of working out what year the date is (it will always be in the future). Something along the lines of:

if (the month in the date string has elapsed) {
    the year of the date is this year + 1
}
4
  • If you are going to get very inconsistent and non-standard formats you will have an impossible job ahead of you. You can work with dates in any format in PHP, but you must know the format to be able to do that. Will you know the format when processing the string? Commented Jul 24, 2014 at 14:26
  • The date strings are coming from a range of places - mainly from APIs provided by arts and cultural venues plus a dash of web scraping where no API is available. Each API / website does use it's own format consistently - the problem is that data is coming in from lots of different places. Commented Jul 24, 2014 at 14:30
  • But each place uses a consistent format? If so, you can handle this for sure. If you confirm I can show you how to do it. Commented Jul 24, 2014 at 14:31
  • Yep, each of the sources is consistent :) Commented Jul 24, 2014 at 14:33

1 Answer 1

3

As long as each source provides you with a consistent format you can use DateTime() and DateTime::createFromFormat() to process the dates for you.

//Saturday 26 July
$date = DateTime::createFromFormat('l j F', 'Saturday 26 July');

//July 24th, 2014
$date = new DateTime('July 24th, 2014');

//Thu 4 Sep
$date = DateTime::createFromFormat('D j M', 'Thu 4 Sep');

//Thu 28 Aug — Fri 19 Sep
list($start, $end) = explode(' - ', 'Thu 28 Aug — Fri 19 Sep');
$start = DateTime::createFromFormat('D j M', $start);
$end   = DateTime::createFromFormat('D j M', $end);

//24-07-2014
$date = new DateTime('24-07-2014');

I'm going to leave handling Monday 28 - Wednesday 30 July to you since you'll need to do a little more work to get the month from the second date and apply it to the first. But this should show you how to go about this.

Sign up to request clarification or add additional context in comments.

1 Comment

That's great - thank you John! Looks like I could very easily use this as part of the API / website scraping so that everything is consistent before it goes into the database. Guessing something along the lines of strrchr("Monday 28 - Wednesday 30 July", " ") would work for the range of dates where the month is only mentioned once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.