Re: date conversions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thank you for all your help.

Among all  the variations I found this to be the clearest:

list($d,$m,$y) = explode("/",$testdate);
$mysqldate = date("Y-m-d", mktime(0,0,0,$m,$d,$y));

But I also thought the use of split instead of explode so you could nominate
multiple delimiters was good.
eg.
list($d,$m,$y) = split('[/.-]', $testdate);

Another variation was to hardcode the date results instead of using the date
function which is very simple:
eg.
$mysqldate = $y.'-'.$m.'-'.$d;

Sorry for asking a question that is obviously asked often.

Interesting comment on 'normal'. I suspect that numerically more of the
world uses dmy than mdy. There is a lot more of the world outside the US
than in it.

Thanks again for all your help.

Neil


<tg-php@xxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:E1Cep8N-0002VY-6p@xxxxxxxxxxxxxxxxxxxxx
> Yeah, this is my problem with relyinig on strtotime().  If you don't give
it a format that it knows, it's going to give you random results (well, not
random, but "undesireable").  Seems like more of a crutch that leaves too
much chance of error for my taste.  I prefer to be a little more explicit:
>
>
> $testdate="30/11/2004";
> list($day,$month,$year) = explode("/",$testdate);
> echo date("Y-m-d", mktime(0,0,0,$month,$day,$year));
>
> Try that out.  mktime() produces a serial date/time just like strtotime()
but you have a little more control over what it's producing and subsequently
what gets piped into date().  This exact example is what I used once before
when arguing against using strtotime().  Most people are going to use a
'normal' format that strtotime() likes, but the format you're using
(european standard?) and just using the numbers is the one big instance that
strtotime() breaks.
>
> Hope this helps.  You should be able to get whatever date format you need
now.
>
> -TG
>
> *** new email address tg-php@xxxxxxxxxxxxxxxxxxxxxx
> *** old email address tgryffyn@xxxxxxxxxxxxxxxxx YAY CHAPTER 11!  :(
>
>
>
>
> = = = Original message = = =
>
> Hi
>
> I am needing to convert a d/m/y date such as 30/11/2004 into the format
that
> mysql can use ie. 2004-11-20
>
> If I try the following:
>
> $testdate="30/11/2004";
> echo date("Y-m-d", strtotime($testdate));
>
> the result is - 2006-06-11
>
> I can't find any other function apart from strtotime to do this.
>
> Any ideas?
>
> Thanks
> Neil
>
>
> ___________________________________________________________
> Sent by ePrompter, the premier email notification software.
> Free download at http://www.ePrompter.com.

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux