Re: str_to_date equivalent in PHP
| From: | Ashley Sheridan | Date: | Tue, 28 Jul 2009 17:32:43 +0000 |
| Subject: | Re: str_to_date equivalent in PHP | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-295955@lists.php.net to get a copy of this message | ||
On Tue, 2009-07-28 at 20:10 +0300, Thodoris wrote:
> > 2009/7/28 Thodoris <tgol@kinetix.gr>:
> >
> >> Hi gang,
> >> I've been looking for a str_to_date (mysql) equivalent in PHP. I've
> >> noticed that these are matching the description:
> >>
> >>
> >> http://www.php.net/manual/en/datetime.createfromformat.php
> >>
> >> http://www.php.net/manual/en/function.date-create-from-format.php
> >>
> >> but I don't have PHP 5.3.0 installed in any of my systems to test it and the
> >> function/method is not well documented yet. So I will have to write a
> >> workaround this.
> >>
> >> Has anybody tried this?
> >>
> >
> > Does strtotime() not work for you?
> >
> >
> Well actually it doesn't basically because I need to define the date's
> format. This is because strtotime will use for this date:
> 7/8/2009
> the *month/day/year* format but in Greece we usually write dates in
> *day/**month/year* so this is causing me trouble.
>
> I have written this in case there is an active database handler around:
>
> function db_date2mysql($date_str,$date_format="%d/%m/%Y",$dbh=null) {
> if (isset($dbh)) {
> $sql = "SELECT STR_TO_DATE('$date_str','$date_format') AS
>
date";
> $ar = $dbh->query($sql)->fetch(PDO::FETCH_ASSOC);
> return $ar['date'];
> } else {
> return null;
> }
> }
>
> but I will need something more solid.
>
I've always used strtotime from the output I get from the database, and
it's always worked for me, and before you ask, I live in the UK where
the date formats make sense :p
Thanks
Ash
www.ashleysheridan.co.uk