简体   繁体   English

PHP从单独的日期和时间获取时间戳

[英]PHP get timestamp from separate date and time

I have two inputs, one for the date in yyyy/mm/dd format and another for time in 12:15 AM. 我有两个输入,一个输入为yyyy / mm / dd格式的日期,另一个输入为12:15 AM的时间。 Now I need to save into the the databse a timestamp. 现在,我需要将时间戳记保存到数据库中。 I get both inputs lets say: 我得到两个输入,可以说:

$my_date = '2013/12/22';
$my_time = '12:50 PM';

How do I get a timestamp to save into db? 如何获取时间戳以保存到数据库?

Thank you 谢谢

Give this a try... 试试这个...

$my_date = '2013/12/22';
$my_time = '12:50 PM';

$full_date = $my_date . ' ' . $my_time;

$timestamp = strtotime($full_date);

Use DateTime:createFromFormat() 使用DateTime:createFromFormat()

<?php

$my_date = '2013/12/22';
$my_time = '12:50 PM';

$d = sprintf('%s %s', $my_date, $my_time);

$dt = DateTime::createFromFormat('Y/m/d h:i A',  $d);
$ts = $dt->getTimestamp();

var_dump($ts);

Yields: 产量:

int 1387716600

Hope this helps :) 希望这可以帮助 :)

Edit 编辑

You could use strtotime() to convert it into an UNIX timestamp. 您可以使用strtotime()将其转换为UNIX时间戳。

Eg: 例如:

$my_date = strtotime('2013/12/22');
$my_time = strtotime('12:50 PM');

Full date + time timestamp 全日期+时间时间戳

$timestamp = strtotime('2013/12/22 12:50 PM');

More info: http://php.net/manual/en/function.strtotime.php 更多信息: http : //php.net/manual/zh/function.strtotime.php

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

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