1,105,214 Community Members

Two functions to convert date

Member Avatar
(MitkOK)
Reputation Points: 61 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello.

I know that there are many ways to convert date in PHP, but i use those to two convert from/to MySQL DB.

// this function convert date from YYYY-MM-DD to DD-MM-YYYY in order to use in HTML table for example

function mysql2table($date) {
 	
          $new = explode("-",$date);
          $a=array ($new[2], $new[1], $new[0]);

          return $n_date=implode("-", $a);
 }
 
// this function convert date from DD-MM-YYYY to YYYY-MM-DD in order to use in MySQL table in DATE format for example

 function table2mysql($year,$month,$day) {

         if ($day<=9) { $day="0".$day; }
         if ($month<=9) { $month="0".$month; }
         $a=array ($year, $month, $year);

         return $n_date=implode("-", $a);
 }
Member Avatar
jishu
Newbie Poster
2 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

you have any idea where i learn PHP first time?
I have only HTML idea

Member Avatar
Ginetta
Newbie Poster
5 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

You could use the date format at the data source... when you run the sql query:

SELECT date_format(date_column, '%M %D, %Y') as date FROM table;

Member Avatar
asimshahiddIT
Newbie Poster
7 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

<?php
$0
$new_date=date("Y-m-d",strtotime($old_date));
?>
please the format to any format

You
Post:
Start New Discussion
Tags Related to this Article