开发者

Convert date format for db [duplicate]

开发者 https://www.devze.com 2023-04-11 20:54 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: Convert one date format into another in PHP
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Convert one date format into another in PHP

I have a date in the following format DD, d MM, yy (Friday, 21 October, 2011) how do I reformat it t开发者_开发知识库o store in mySQL db in the date format (0000-00-00)?


Use the date function:

$time = strtotime("Friday, 21 October, 2011");
echo date("Y-m-d", $time);

The linked page provides a good description of the available format specifiers, and you can see the predefined datetime constants here.


Format           Description
================================================================================
d                Day of the month, 2 digits with leading zeros
D                A textual representation of a day, three letters
j                Day of the month without leading zeros
l                A full textual representation of the day of the week
N                ISO-8601 numeric representation of the day of the week
S                English ordinal suffix for the day of the month, 2 characters
w                Numeric representation of the day of the week
z                The day of the year (starting from 0)
Week             ---------------------------------------------------------------
W                ISO-8601 week number of year, weeks starting on Monday
Month            ---------------------------------------------------------------
F                A full textual representation of a month, such as January or March
m                Numeric representation of a month, with leading zeros
M                A short textual representation of a month, three letters
n                Numeric representation of a month, without leading zeros
t                Number of days in the given month
Year             ---------------------------------------------------------------
L                Whether it's a leap year
o                ISO-8601 year number.
Y                A full numeric representation of a year, 4 digits
y                A two digit representation of a year
Time             ---------------------------------------------------------------
a                Lowercase Ante meridiem and Post meridiem
A                Uppercase Ante meridiem and Post meridiem
B                Swatch Internet time
g                12-hour format of an hour without leading zeros
G                24-hour format of an hour without leading zeros
h                12-hour format of an hour with leading zeros
H                24-hour format of an hour with leading zeros
i                Minutes with leading zeros
s                Seconds, with leading zeros
u                Microseconds (added in PHP 5.2.2)
Timezone         ---------------------------------------------------------------
e                Timezone identifier (added in PHP 5.1.0)
I                Whether or not the date is in daylight saving time
O                Difference to Greenwich time (GMT) in hours
P                Difference to Greenwich time (GMT) with colon between h and m
T                Timezone abbreviation
Z                Timezone offset in seconds.
Full Date/Time   ---------------------------------------------------------------
c                ISO 8601 date (added in PHP 5)
r                » RFC 2822 formatted date
U                Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)


You can use date:

date( "Y-m-d", strtotime( $date ) );

-> http://php.net/manual/en/function.date.php


Your date format DD, d MM, yy doesn't tally with this (Friday, 21 October, 2011. Perhaps you mean date('l, d F, Y')

echo date( "Y-m-d", strtotime( date('l, d F, Y') ) );

Outputs

2011-10-08
0

精彩评论

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