Converting from one format to Another
"I have got a date in DD / MM / YYYY but I need it in MM-DD-YYYY. Help!"
How do we help this person? How do we help this person? Well, let's analyze the problem a bit. Well, let's Analyze the problem a bit. Each date consists of three components: the day, the month and the year. Each date consiste of three components: the day, the month and the Year. So, essentially what we need to do is reorder it and use hyphens instead of slashes. So, essentially what we need to do is reorder it and instead use hyphens or slashes. There are a few ways we can accomplish this. There are a few ways we can 'accomplish this. Let's first try splitting it up. Let's first try splitting it up. We've got a function called explode() for that. We've got a working Called explode () for that.
$oldDate = '18/08/2009'; // DD/MM/YYYY $ OldDate = '18 / 08/2009 ', / / DD / MM / YYYY
$parts = explode('/', $oldDate); $ Parts = explode ('/', $ oldDate);
/** / **
* Now we have: * Now we have:
* $parts[0]: the day * $ Parts [0]: the day
* $parts[1]: the month * $ Parts [1]: the month
* $parts[2]: the year * $ Parts [2]: the Year
* We could also have done: * We Could Also Have done:
* list($day, $month, $year) = explode('/', $oldDate); * List ($ day, $ month, $ year) = explode ('/', $ oldDate);
*/ * /
$newDate = "{$parts[1]}-{$parts[0]}-{$parts[2]}"; // MM-DD-YYYY $ NewDate = "($ parts [1 ]}-{$ parts [0 ]}-{$ parts [2]) '/ / YYYY-MM-DD
echo $newDate; // 08-18-2009 echo $ newDate / / 08-18-2009
Seems pretty nice to this work. We can also use something called regular expressions. We Can Also Called something use regular expressions. This will essentially be a one-liner: Will this be essentially a one-liner:
$oldDate = '18/08/2009'; $ OldDate = '18 / 08/2009 ';
$newDate = preg_replace('#^(\d{2})/(\d{2})/(\d{4})$#', '$2-$1-$3', $oldDate); $ NewDate = preg_replace ('#^( \ d (2)) / (\ d (2)) / (\ d (4 })$#', '$ 2 - $ 1 - $ 3' $ oldDate); I am not going to explain regular expressions in this tutorial. You may check out this regex tutorial for that. You May check out this tutorial regex for that.
Any other ways? Any Other Ways? Yes, there is! Yes, there is! As it usually happens to be with programming, there are many ways to do just one thing. As it happens to be with programming Usually, there are many ways to do just one thing. Assuming we have already split up our date in three variables $day , $month and $year we can use a function called mktime() for generating a Unix timestamp and then use date() to format it in this way: Assuming we split up our possessions Already date in three variables $ day, $ month and $ year Can we use a function Called mktime () for generating a Unix timestamp and then use date () to format it in this way:
$timestamp = mktime(0, 0, 0, $month, $day, $year); $ Timestamp = mktime (0, 0, 0, $ month, $ day, $ year);
echo date('md-Y', $timestamp); echo date ('md-Y', $ timestamp);
Yet another way is using strtotime () to convert a formatted string Writing a Unix timestamp. EXCEPT, strtotime() does not recognize DD/MM/YYYY as a valid date format, so it returns false. EXCEPT, strtotime () Does Not Recognize DD / MM / YYYY as a valid date format, so it returns false. There are also problems with ambiguity. Also there are problems with ambiguity. Consider the string 07-08-2009. Consider the string 07/08/2009. Is that the 7th of August, or is it the 8th of July? Is that the 7th of August, or is it the 8th of July? It will be the former. Will it be the performer.
1 comment:
very nice, i learnt a lot.
Post a Comment