RE: [PHP-DB] date conversions

2004-12-16 Thread Ford, Mike
To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 16 December 2004 06:00, neil wrote:

 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

strtotime() is, unfortunately, a little American biased and only recognises
the mm/dd/ numeric format (using slashes).  So the above is returning
the equivalent of the 11th day of the 30th month of 2004!

You can use other PHP functions, as has been suggested, but you might also
investigate the mySQL date formatting functions, as I believe they work
perfectly well on input dates as well as output ones.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

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



Re: [PHP-DB] date conversions

2004-12-15 Thread Jason Wong
On Thursday 16 December 2004 14:00, neil wrote:

 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.

Use the string functions to manipulate it into the required format, explode() 
is one approach. If you're desperate search the archives, hundreds of 
variations of code to do this have been posted in the past. But it might be 
quicker to write your own than to hit upon the appropriate keywords to search 
for.

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-db
--
/*
If practice makes perfect, and nobody's perfect, why practice?
*/

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



Re: [PHP-DB] date conversions

2004-12-15 Thread neil
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


[EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 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 [EMAIL PROTECTED]
 *** old email address [EMAIL PROTECTED] 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