1

I'm trying to get my head round someone else's code which they've written for handling the dates of when news stories are published. The problem has come up because they are using this line -

$date = strtotime("midnight", strtotime($dateString));

to process a date selected using a jquery calendar widget. This works fine for future dates, but when you try to use a date which is in the previous calendar year, it uses the current year instead. I think this is due to "midnight" finding the closest instance of the selected day and month.

I could remove the "midnight", but I'm not sure what the repercussions of this would be - is there a reason that the midnight could be there?

EDIT: this is the full block of code which handles the date. The date contains the time, which allows the user to publish an item at a specific time.

            $array['display_date'] = '24 October, 2011 17:30';
            $string = $array['display_date'];
            $dateString = substr($string, 0, -5);

            $timeArray = explode(':', substr($string, -5));
            $hours_in_secs = 60 * 60 * $timeArray[0];
            $mins_in_secs = $timeArray[1];
            $date = strtotime("midnight", strtotime($dateString));

            $timestamp = $date + $hours_in_secs + $mins_in_secs;

            //assign timestamp to validation array
            $array['display_date'] = $timestamp;
            echo $array['display_date']; // Output = 1351094430 (Oct 24 2012 17:00:30)
user1153594
  • 281
  • 2
  • 20

1 Answers1

4

This really depends on what $dateString contains. Assuming your jQuery widget delivered the time portion as well, your colleague likely wanted to remove the time portion. Compare the following:

echo date(DATE_ATOM, strtotime('2010-10-01 17:32:00'));
// 2010-10-01T17:32:00+02:00

echo date(DATE_ATOM, strtotime("midnight", strtotime('2010-10-01 17:32:00')));
// 2010-10-01T00:00:00+02:00

If your widget doesnt return the time portion, I dont see any reason for setting the date to midnight, because it will be midnight automatically:

echo date(DATE_ATOM, strtotime('2010-10-01'));
// 2010-10-01T00:00:00+02:00

Note that all these are dates in the past and they will result in the given year in the past, not the current year like you say. If they do in your code, the cause must be somewhere else.

Will there be repercussions when you change the code? We cannot know. This is just one line of code and we have no idea of any context. Your unit-tests should tell you when something breaks when you change code.

EDIT after update

The codeblock you show makes no sense whatsoever. Ask the guy who wrote it what it is supposed to do. Not only will it falsely return the current year for past years, but it will also give incorrect results for the minutes, e.g.

24 March, 2010 17:30 will be 2012-03-24T17:00:30+01:00

I assume this was an attempt at turning 24 March, 2010 17:30 into a valid timestamp, which is in a format strtotime does not recognize. But the approach is broken. When you are on PHP5.3 use

$dt = DateTime::createFromFormat('d F, Y H:i', '24 March, 2010 17:30');
echo $dt->format(DATE_ATOM); // 2010-03-24T17:30:00+01:00

If you are not on 5.3 yet, go through https://stackoverflow.com/search?q=createFromFormat+php for alternate solutions. There is a couple in there.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Thanks for the reply Gordon. The news items are actually set up to allow the user to publish them at an exact time - so the date and time is selected via the calendar. Just checked this and it works correctly. I'll add the full block of code – user1153594 Feb 28 '12 at 11:40
  • Ok - I'm best to scrap what's there and start it from scratch. Thanks Gordon – user1153594 Feb 28 '12 at 12:02
  • The comma before the year was the main problem, the rest of the code needs tidied up too. – user1153594 Feb 28 '12 at 12:16