humphrey88 0 Report post Posted August 19, 2006 I am trying to convert a date that I take from an email. It is in this format:Wed, 02 Aug 2006 03:59:10 -0700And I want to convert it to:2006-08-02This:trim(substr($date,12,4))."-".trim(substr($date,8,3))."-".trim(substr($date,5,2))...will get me:2006-Aug-02.Any quick php tips to convert the "Aug" to convert to "08"?Thanks! Share this post Link to post Share on other sites
souradipm 0 Report post Posted August 19, 2006 (edited) Quickly and simply, use: <?phpecho date("d/m/y"); ?>It is the format day/month/yearso you could have:<?phpecho date("y/m/d"); ?>To give you your result.~souradipm Edited August 19, 2006 by souradipm (see edit history) Share this post Link to post Share on other sites
vujsa 0 Report post Posted August 19, 2006 strtotime() will convert nearly any date format into a timestamp which can be used to build a date with the date() function. $timestamp = strtotime("Wed, 02 Aug 2006 03:59:10 -0700");$newdate = date("Y-m-d", $timestamp);echo $newdate; Hope This Helps vujsa Share this post Link to post Share on other sites
jagoan 0 Report post Posted August 31, 2006 strtotime() will convert nearly any date format into a timestamp which can be used to build a date with the date() function. $timestamp = strtotime("Wed, 02 Aug 2006 03:59:10 -0700");$newdate = date("Y-m-d", $timestamp);echo $newdate; or you can use this function.. <?phpfunction tanggal($x) { $x = explode(" ",$x); switch ($x[2]) { case "Jan": $bln = "01"; break; case "Feb": $bln = "02"; break; case "Mar": $bln = "03"; break; case "Apr": $bln = "04"; break; case "May": $bln = "05"; break; case "Jun": $bln = "06"; break; case "Jul": $bln = "07"; break; case "Aug": $bln = "08"; break; case "Sep": $bln = "09"; break; case "Oct": $bln = "10"; break; case "Nov": $bln = "11"; break; case "Dec": $bln = "12"; break; } $tanggal = "$x[3]-$bln-$x[1]"; return $tanggal;}$tgl = "Wed, 02 Aug 2006 03:59:10 -0700";echo $tgl."<br />";echo tanggal($tgl)."<p>";?> Share this post Link to post Share on other sites
minnieadkins 0 Report post Posted August 31, 2006 (edited) I hate dealing with dates. Just too messy. As noted i guess the best way would be to conver as vujsa said, but you could also use that function written that simple contains an array.Or include this array $DateArr = new array('Jan'=>1,'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6,'Jul'=>7,'Aug'=>8,'Sep'=>9,'Oct'=>10,'Nov'=>11,'Dec'=>12); and then do thistrim(substr($date,12,4))."-".$DateArr[trim(substr($date,8,3))]."-".trim(substr($date,5,2))I typed that on the fly dunno if there's any errors, but if so i'm sure you could easily fix them. Obviously I would just be using a key=>index approach creating a hash table. That's if you used 3 characters per each month. Some pple prefer to write out July and March and April. Whatever approach works best for you.-Good luck Edited August 31, 2006 by minnieadkins (see edit history) Share this post Link to post Share on other sites
vujsa 0 Report post Posted August 31, 2006 $timestamp = strtotime("Wed, 02 Aug 2006 03:59:10 -0700");$newdate = date("Y-m-d", $timestamp);echo $newdate; I feel bad now that my solution onlyy took thre very small lines of code to write. Everyone elses method required so much more code and looked so glamorous. Honestly, I never even thought to do it all manually. I know that my idea completely abandons the whole substr() method that Humphrey1988 had started to use but again, didn't even think of using a bunch of string methods for a date question. I know, I'm in a wierd mood today. I just thought it was funny that there are so many similar solutions to Humphrey1988's problem but most use string funtions instead of data functions. I also thought it was funny that Humphrey1988 posted his question and many working solutions have been provided but no repy was ever given. Not even a "I found my solution at php.net but thanks anyway!" reply. I wrote my solution to be easy to read which is why it takes three line for a single line of code:echo date("Y-m-d", strtotime("Wed, 02 Aug 2006 03:59:10 -0700")); Now I went and did it. That really looks bad. I mean one tiny little line of code. I almost feel bad about posting it. Kind of shameful to offer such a simple solution among such complex and eloquent replies.vujsa Share this post Link to post Share on other sites