datetime to timestamp(日期时间到时间戳)
问题描述
可能的重复:
反日期函数?- 不是strtotime
是否有可能从这样的日期 2011-02-27 02:04:46 获取 UNIX 时间?
你好,
我们有这个函数可以将时间戳转换为日期时间:
we have this function to convert a timestamp to datetime:
$datetime = date('Y-m-d H:i:s', $timestamp);
是否有相反的功能?
日期时间到时间戳.
谢谢
推荐答案
$timestamp = strtotime($datetime);
或者,如果您对格式有信心,请使用 explode()
甚至 substr
拆分字符串,并将必要的部分传递到 mktime
函数.
Or if you're confident of the format, split up the string with explode()
or even substr
and pass the necessary parts into the mktime
function.
请注意,strtotime
有时会导致时间戳错误,如果使用稍微不合常规的格式.
Be aware that strtotime
can sometimes get the timestamp wrong, if a slightly unconventional format is used.
如果你知道你的输入格式,一个真正准确的方法是使用 DateTime::createFromFormat
例如:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat
eg:
$dateTimeObject = DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
请参阅 http://php.net/manual/en/function.date.php 了解格式指南,以及 http://php.net/manual/en/datetime.createfromformat.php有关上述方法的信息.
See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.
这篇关于日期时间到时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:日期时间到时间戳


基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01