如何PHP转换这个时间格式?

时间:2012-08-13 09:10:06

标签: php date

我从数据库中的表中获取此日期,就像这种格式

Sunday 16th of January 2011 06:55:41 PM

我希望将其转换为格式

11-05-2012

如何使用日期函数或任何函数

当我使用日期功能时

 <td><?php echo date('d-m-Y', $v['v_created']); ?></td>

我收到错误消息

 'Severity: Warning

Message: date() expects parameter 2 to be long, string given'

5 个答案:

答案 0 :(得分:2)

这适合我(仅在本地网络服务器上测试)

<?php

date_default_timezone_set ('Europe/Rome');

$date = "Sunday 16th of January 2011 06:55:41 PM";

//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);

$sql_friendly_date = date('y-m-d H:i', strtotime($date));

echo $sql_friendly_date;

?>

您可以根据以下内容格式化日期,更改Date函数的第一个参数:http://it2.php.net/manual/en/function.date.php

答案 1 :(得分:1)

您有以下格式:

 Sunday 16th of January 2011 06:55:41 PM

这是一种基于字符串的格式,因此日期信息或多或少以人类可读的格式编码。幸运的是用英语。让我们看看,这是由空格分隔的多个事物:

 Sunday   - Weekdayname
 16th     - Date of Month, numeric, st/nd/th form
 of       - The string "of".
 January  - Monthname 
 2011     - Year, 4 digits
 06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
 PM       - AM/PM

因此,您可以按空格分隔每个节点,然后分析数据。您只需要所有月份名称和sscanf功能,因为您只需要月份,月份和年份:

$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);

这将为您提供以下变量:

$monthname - string(7) "January"
$day       - int(16)
$year      - int(2011)

所以剩下要做的就是将月份名转换为一个数字,这个数字可以用地图(用PHP中的数组形式)和一些格式化的输出来完成:

$monthnames = array(
    'January' => 1,
    # ...
);

printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);

因此,无论出现哪个问题,只要输入格式一致,您就可以将其拆开,处理获得的数据并根据需要进行输出。这就是它的工作原理。

答案 2 :(得分:0)

试试这个。总是为我工作

$date = Sunday 16th of January 2011 06:55:41 PM

$new_date = date('d-M-Y', strtotime($date));

echo $new_date;

答案 3 :(得分:0)

您使用的格式Sunday 16th of January 2011 06:55:41 PM是一种错误的格式。从您插入的表格中,此日期在数据库中的日期(Ymd)应该是插入数据库中的日期值,如: - 11-05- 2012。你可以获取它并获得你想要的格式。

答案 4 :(得分:0)

<?php

$old_date = date('l, F d y h:i:s');         // returns Saturday, January 30 10 02:06:34

$new_date = date('d-M-Y', strtotime($old_date));

echo $new_date
?>




     more details about date plz visit this url

    http://www.php.net/manual/en/datetime.createfromformat.php
相关问题