开发者

mysql date formatting with php

开发者 https://www.devze.com 2023-03-09 08:29 出处:网络
I have a date (\"mm/dd/yyyy\") and I want to convert it to a MySQL DATE data type (like yyyy-mm-dd) How do I do i开发者_StackOverflowt with PHP?Take a look at mysql function str_to_date()

I have a date ("mm/dd/yyyy") and I want to convert it to a MySQL DATE data type (like yyyy-mm-dd)

How do I do i开发者_StackOverflowt with PHP?


Take a look at mysql function str_to_date()

example

select str_to_date('10/30/2010','%m/%d/%Y') -- 2010-10-30


Nick rulez's answer also applies to inserts and updates:

INSERT INTO my_table (id, date) values (1, str_to_date('10/30/2010','%m/%d/%Y'))


Lots of ways to do it. I like always converting my dates to a timestamp cause I find it easiest to then do what I want.

In which case:

<?php
echo date( "Y-m-d",strtotime("09/02/1988"));
?>

http://codepad.viper-7.com/Z9vDv7


If your date is in $your_date, then:

$mysql_date = date('Y-m-d', strtotime($your_date));

See strtotime() documentation.


$date = '12/25/2011';
$parts = explode('/', $date);
$sql_date = $parts[2] . '-' . $parts[0] . '-' . $parts[1];


function date2mysql($date) {

   list($month, $day, $year) = explode('/', $date);
   $timestamp = mktime(0, 0, 0, $month, $day, $year);
   return date("Y-mm-d",$timestamp);
}

see the date manual for the format

0

精彩评论

暂无评论...
验证码 换一张
取 消