简体   繁体   English

如何将带有日期和时间AM / PM的字符串转换为24小时的mysql时间戳格式

[英]How to convert string with date and time AM/PM to 24 Hour mysql timestamp format

I am trying to insert date and time into mysql datetime field from a string having following dd/mm/yyyy hh:mm:ss AM/PM format: 我试图从具有以下dd / mm / yyyy hh:mm:ss AM / PM格式的字符串中将日期和时间插入到mysql datetime字段中:

20/10/2014 05:39 PM

20/10/2014 05:39 AM

I know MYSQL timestamp format is yyyy-mm-dd hh:mm:ss or 0000-00-00:00:00:00 我知道MYSQL时间戳格式是yyyy-mm-dd hh:mm:ss or 0000-00-00:00:00:00

So if I do: 所以,如果我这样做:

$s = substr("20/10/2014 05:39 PM", 0, 10);
$h = date("G:i", strtotime($s));
list($day, $month, $year, $hour, $minute) = split('[/ :]', "20/10/2014 05:39 PM"); 
echo $d1me = $year . '-' . $month. '-' .  $day . ' ' . $h;

I get 2014-10-20 19:00 我得到2014-10-20 19:00

So I guess there is a problem with date_default_timezone_set() function, How to solve this and get expected result? 所以我想date_default_timezone_set()函数存在问题,如何解决这个问题并获得预期结果?

20/10/2014 05:39 PM     ->   2014-10-20 17:39:00

20/10/2014 05:39 AM     ->   2014-10-20 05:39:00

How to do it? 怎么做?

MySQL already knows how to parse many different types of date strings natively, using the STR_TO_DATE() function in combination with format strings used by DATE_FORMAT() . MySQL已经知道如何使用STR_TO_DATE()函数结合DATE_FORMAT()使用的格式字符串本地解析许多不同类型的日期字符串。

So, I would not involve PHP in this process at all, and instead allow MySQL to parse the input itself. 所以,我根本不会在这个过程中涉及PHP,而是允许MySQL解析输入本身。

The format string you need to use is %d/%m/%Y %h:%i %p , where the %p represents AM/PM . 您需要使用的格式字符串是%d/%m/%Y %h:%i %p ,其中%p表示AM/PM

You can use the entire expression right in your INSERT statement, passing the strings directly from PHP assuming you have validated their format already. 您可以在INSERT语句中使用整个表达式,直接从PHP传递字符串,假设您已经验证了它们的格式。

INSERT INTO your_table (timestamp_column) VALUES (STR_TO_DATE('20/10/2014 05:39 PM', '%d/%m/%Y %h:%i %p'));

...will correctly insert the DATETIME value 2014-10-20 17:39:00 into your table. ...将正确插入DATETIME2014-10-20 17:39:00到您的表中。

If you really prefer to do it in PHP first, use DateTime::createFromFormat() (PHP 5.3+) using the format string 'd/m/YH:i A' 如果您真的更喜欢首先在PHP中使用它,请使用格式字符串'd/m/YH:i A'使用DateTime::createFromFormat() (PHP 5.3+)

$d = DateTime::createFromFormat('d/m/Y H:i A', '20/10/2014 05:39 PM');
var_dump($d);

class DateTime#2 (3) {
  public $date =>
  string(26) "2014-10-20 17:39:00.000000"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(15) "America/Chicago"

To get a MySQL formatted date back out of it, 要从中获取MySQL格式的日期,

echo $d->format('Y-m-d H:i:s');
// 2014-10-20 17:39:00

If you are in the deeply unfortunate situation of having a PHP version older than 5.3, you can achieve similar results with strptime() but you'll need to assemble its array output into MySQL's string format. 如果您处于使用早于5.3的PHP版本的非常不幸的情况下,您可以使用strptime()获得类似的结果,但是您需要将其数组输出组装成MySQL的字符串格式。

This should work for you: 这应该适合你:

<?php

    $input = "20/10/2014 05:39 AM";  //20/10/2014 05:39 PM

    list($day, $month, $year, $hour, $minute, $dayType) = preg_split('/[\/\s:]+/', $input); 
    echo $d1me = $year . '-' . $month. '-' .  $day . ' ' . ($dayType == "PM"?$hour+12: $hour) . ":" . $minute . ":00";

?>

Output: 输出:

2014-10-20 05:39:00  //2014-10-20 17:39:00

I think you can use the following code. 我想你可以使用以下代码。 It's easier to understand: 它更容易理解:

echo date("Y-m-d H:i", strtotime("20/10/2014 05:39 PM"));

如果你使用PHP,这是更好的方法

echo date('Y-m-d H:i', strtotime('20/10/2014 05:39 PM'));

echo date("Ymd H:i:s", strtotime("20-10-2014 05:39 PM")); 回音日期(“Ymd H:i:s”,strtotime(“20-10-2014 05:39 PM”));

Use above code - but keep in mind you have "-" in place of "/" in input date. 使用上面的代码 - 但请记住,在输入日期中您使用“ - ”代替“/”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM