Re: date format

From: Date: Mon, 22 Apr 2002 13:01:16 +0000
Subject: Re: date format
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-93976@lists.php.net to get a copy of this message
Hamish, A quick read through the date functions in the manual is worthwhile http://www.php.net/manual/en/ref.datetime.php In particular, you need to look at strtotime() and date(). strtotime() is an often overlooked function, useful for getting almost any english written date into a unix timestamp, then use date() to get it into the format you wish. Note, I removed the commas from the $date string, because I'm pretty sure it will break the strtotime() function. <? $date = "April 11, 2002, 1:53 pm"; // one step at a time $date = str_replace(",","",$date); $date = strtotime($date); $date = date('Y-m-d', $date); // or, in one line, if that's your thing $date = date('Y-m-d', strtotime(str_replace(",","",$date))); ?> Alternatively, you could have split the date on commas, and mucked around with each element, but the above, IMHO, is the best way to go. Regards, Justin French -------------------- Creative Director http://Indent.com.au -------------------- on 22/04/02 10:10 PM, hamish (hamish@hamishprice.co.uk) wrote: > Hello All, > I have a variable in the following date format: > > April 11, 2002, 1:53 pm > > and want to get it to the following format: > > yyyy-mm-dd > > what's the best way? > > hamish

« previous php.general (#93976) next »