homepage Welcome to WebmasterWorld Guest from 23.22.128.96
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Can't work out what's going on with dates?
dd/mm/yy -> mm/dd/yy?
Kenton




msg:3104791
 2:35 am on Oct 2, 2006 (gmt 0)

Hello

I'm trying to format some user input of dates.

I am using PHP 4.4.2

Running this
<?
$fred = strftime("%d/%m/%y",strtotime('11/12/07'));
echo "$fred";
?>

produces:

12/11/07

I am going nuts with this

Please help

 

DrDoc




msg:3104799
 3:06 am on Oct 2, 2006 (gmt 0)

I take it you want to use Dec 11. Well, if so -- it's best to always use ISO dates, until they are printed. Using a non-ISO date means that you have to worry about locales and other such things, which is what you are running into here. You are passing "11/12/07" ... obviously meaning "11 December, 2007" ... But, due to the server locale, it will treat it as "November 12, 2007". Using an ISO date solves this:

<?
$fred = strftime("%d/%m/%y",strtotime('2007-12-11'));
echo "$fred";
?>

OR, prior to the "strtotime" line -- use setlocale to modify the locale used by the script.

Kenton




msg:3104806
 3:16 am on Oct 2, 2006 (gmt 0)

Thanks DrDoc

(yes I did mean December)

But ... already tried setlocale
<?
setlocale (LC_TIME, 'en_GB');
$fred = strftime("%d/%m/%y",strtotime('11/12/07'));
echo "$fred";
?>

produces the same result.

Also, as this is user input, I would prefer to stick to the common dd/mm/yy format.

thanks for any help

DrDoc




msg:3104852
 4:03 am on Oct 2, 2006 (gmt 0)

strtotime [php.net]
The function expects to be given a string containing a US English date format...

See also Date Input Formats [gnu.org], which is what strtotime follows.

DrDoc




msg:3104856
 4:04 am on Oct 2, 2006 (gmt 0)

Since it's user input -- perhaps you should use dropdowns to select days/months?

Kenton




msg:3104891
 5:15 am on Oct 2, 2006 (gmt 0)

Thanks. Will use dropdowns.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved