如何从例如22-09-2008中获取时间戳?
当前回答
对于PHP >=5.3, 7和8,这可能工作-
$date = date_parse_from_format('%Y-%m-%d', "2022-11-15"); //here you can give your desired date in desired format.
//just need to keep in mind that date and format matches.
$timestamp = mktime(0, 0, 0, $date['month'], $date['day'], $date['year'] + 2000); //this will return the timestamp
$finalDate= date('Y-m-d H:i:s', $timestamp); //now you can convert your timestamp to desired dateTime format.
文档:
date_parse_from_format () mktime () 日期()
其他回答
使用strtotime()函数,您可以轻松地将日期转换为时间戳
<?php
// set default timezone
date_default_timezone_set('America/Los_Angeles');
//define date and time
$date = date("d M Y H:i:s");
// output
echo strtotime($date);
?>
更多信息:http://php.net/manual/en/function.strtotime.php
在线转换工具:http://freeonlinetools24.com/
<?php echo date('U') ?>
如果你愿意,把它放在一个MySQL输入类型时间戳中。上面的代码运行得很好(仅适用于PHP 5或更高版本):
<?php $timestamp_for_mysql = date('c') ?>
使用mktime:
list($day, $month, $year) = explode('-', '22-09-2008');
echo mktime(0, 0, 0, $month, $day, $year);
如果你想把UTC日期时间(2016-02-14T12:24:48.321Z)转换成时间戳,下面是你该怎么做:
function UTCToTimestamp($utc_datetime_str)
{
preg_match_all('/(.+?)T(.+?)\.(.*?)Z/i', $utc_datetime_str, $matches_arr);
$datetime_str = $matches_arr[1][0]." ".$matches_arr[2][0];
return strtotime($datetime_str);
}
$my_utc_datetime_str = '2016-02-14T12:24:48.321Z';
$my_timestamp_str = UTCToTimestamp($my_utc_datetime_str);
对于PHP >=5.3, 7和8,这可能工作-
$date = date_parse_from_format('%Y-%m-%d', "2022-11-15"); //here you can give your desired date in desired format.
//just need to keep in mind that date and format matches.
$timestamp = mktime(0, 0, 0, $date['month'], $date['day'], $date['year'] + 2000); //this will return the timestamp
$finalDate= date('Y-m-d H:i:s', $timestamp); //now you can convert your timestamp to desired dateTime format.
文档:
date_parse_from_format () mktime () 日期()
推荐文章
- 如何从一个查询插入多行使用雄辩/流利
- 如何使用yyyyMMddHHmmss格式格式化当前时间?
- SQL Developer只返回日期,而不是时间。我怎么解决这个问题?
- 在mongodb中存储日期/时间的最佳方法
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- PHP中的echo, print和print_r有什么区别?
- 前一个月的Python日期
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 如何在python中验证日期字符串格式?
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false