1

I have a custom Moodle plugin that somebody else wrote. A user enters a date in the plugin and that date gets recorded against a learning activity.

Originally I had a problem where the plugin would mistake some dates and get confused between AU(UK) date format and US date format. I fixed that problem using the solution from here (How to make strtotime parse dates in Australian (i.e. UK) format: dd/mm/yyyy?).

But now a weird error pops up every now and then where certain dates (notably July 2nd, 2015) gets it's year and day transposed. So 02-07-2015 gets recorded as 15-07-2002.

It doesn't appear to be an AU vrs US date thing because that should just flip the day and month, not the year. Also, it only happens with that specific date. 01-07-2015 and 03-07-2015 work fine.

What's even more frustrating is that I can't reproduce this on our staging server. The problem only occurs in our production server, even though they are both running the same code and same version of Moodle.

What I can share of the code is how I am processing the date, which is...

$date[$i] = strtotime(str_replace('/', '-', $date[$i]));

This has got me wondering if there could be some PHP setting that could be causing this. Or something else I'm over looking?

FYI, the production server is maintained by a paid hosting provider and I don't have access to the php.ini file (but I do on staging).

Has anyone has issues like this? Or can you suggest where I could start looking to find the problem?

Community
  • 1
  • 1
Lucien Stals
  • 237
  • 3
  • 15
  • 1
    plz xplain with code – Bugfixer Jul 17 '15 at 05:43
  • 1
    So how exactly you parse the date and how do you check it's parsed incorrectly? – zerkms Jul 17 '15 at 05:45
  • Like I said, somebody else wrote this plugin, so it's not mine to share. Also there's a LOT of code, and if the bug is in the code, I have no idea which bit is relevant. – Lucien Stals Jul 17 '15 at 05:47
  • hey have you checked current datetime and timezone on your both server. – Bugfixer Jul 17 '15 at 05:50
  • Ok, I've updated the question with how I parse the dates, but I don't think that's where the problem is. @Bugfixer the timezones are slightly different (one is Melbourne, the other is Perth), but both are in AU. – Lucien Stals Jul 17 '15 at 05:53
  • @LucienStals - thats really weird thing never happened with me. – Bugfixer Jul 17 '15 at 06:04
  • There is a an [answer here for you](http://stackoverflow.com/questions/6238992/converting-string-to-date-and-datetime/6239010#6239010) – Ibu Jul 17 '15 at 06:30

1 Answers1

0

try to adjust your format in date function like below, in place of using str_replace

if $dat = '2015-07-17';
$date= date('d-m-Y', strtotime($dat));

OR

$date= date('d:m:Y', strtotime($dat));

OR

$date= date('d/m/Y', strtotime($dat));

OR

$date= date('m-d-Y', strtotime($dat));
Hytool
  • 1,358
  • 1
  • 7
  • 22