Quantcast
Channel: php – Windows – Linux – Mac OSX Guides
Viewing all articles
Browse latest Browse all 11

Converting between PHP & MySql DateTime

$
0
0

I work most of the time in PHP and here are two functions I found to be helpful most of the time when dealing with DateTime value type between PHP & MySQL
(It’s not written by me, but shared by a friend of mine) So here to share with you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* Convert PHP DateTime to MySQL DateTime
* e.g: 20.10.2009 11:15:30 => 2009-10-20 11:15:30 
*/
function php2mysql_datetime($php_datetime){
	return date('Y-m-d H:i:s', strtotime($php_datetime));
}
 
/*
* Convert MySQL DateTime to PHP DateTime
* e.g: 2009-10-20 11:15:30
*/
function mysql2php_datetime($mysql_datetime){
	# getting the date
	$d = split(' ', $mysql_datetime);
	if($d && count($d)>1){
		list($year, $month, $day) = split("-", $d[0]);
		list($hour, $minute, $second) = split(":", $d[1]);
		$d = date('d.m.Y H:i:s', mktime($hour, $minute, $second, $month, $day, $year));}
	else if($d & count($d)==1){
        	list($year, $month, $day) = split("-", $d[0]);
        	$d = date('d.m.Y', mktime(0, 0, 0, $month, $day, $year));
    	} else {
        	$d = NULL;
    	}
	return $d;
}

Share


Viewing all articles
Browse latest Browse all 11

Trending Articles