note 90024 modified in function.strtotime by danbrown

From: Date: Thu, 02 Apr 2009 13:33:02 +0000
Subject: note 90024 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-152757@lists.php.net to get a copy of this message
Fails for non-US dates where the ordering is uncertain, such as 01/02/2003 - parses this as Feb 1st, rather than Jan 2nd. If you are parsing dates for a non-US locale, you can flip these elements of your date: <?php $y = $_POST['date']; if (preg_match('/^\s*(\d\d?)[^\w](\d\d?)[^\w](\d{1,4}\s*$)/', $y, $match)) { $y = $match[2] . '/' . $match[1] . '/' . $match[3]; } echo date('d # m # Y', strtotime($y)); ?> WARNING: Above only works for dates, and breaks for times: 12:30:01 will be converted to 30/12/01. This absolutely ought to be a flag to strtotime(), either as a boolean "monthfirst" flag, or a boolean "respect my locale" flag. Since it isn't, kludge is needed. --was-- Fails for non-US dates where the ordering is uncertain, such as 01/02/2003 - parses this as Feb 1st, rather than Jan 2nd. If you are parsing dates for a non-US locale, you can flip these elements of your date: $y = $_POST['date']; if (preg_match('/^\s*(\d\d?)[^\w](\d\d?)[^\w](\d{1,4}\s*$)/', $y, $match)) { $y = $match[2] . '/' . $match[1] . '/' . $match[3]; } echo date('d # m # Y', strtotime($y)); WARNING: Above only works for dates, and breaks for times: 12:30:01 will be converted to 30/12/01. This absolutely ought to be a flag to strtotime(), either as a boolean "monthfirst" flag, or a boolean "respect my locale" flag. Since it isn't, kludge is needed. http://php.net/manual/en/function.strtotime.php

« previous php.notes (#152757) next »