转换unix时间戳php

发布于 2024-09-18 16:05:13 字数 502 浏览 1 评论 0原文

我有一个数据库来存储我的时间。我使用 PHP 将其插入,

date( 'Y-m-d H:i:s');

然后使用此函数将其转换为 PHP 中的 unix 时间戳

function convert_datetime($str) 
{
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);

$timestamp = mktime($hour, $minute, $second, $month, $day, $year);

return $timestamp;
}

我现在需要做的就是将此时间戳转换为以下格式的日期和时间: yyyymmddhhmmss(没有任何连字符、破折号、冒号等)。

有人有什么想法吗?

I have a database that stores the time for me. I insert it from PHP using

date( 'Y-m-d H:i:s');

I then use this function to convert it to a unix timestamp in PHP

function convert_datetime($str) 
{
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);

$timestamp = mktime($hour, $minute, $second, $month, $day, $year);

return $timestamp;
}

What I now need to do is convert this timestamp to the date and time in this format:
yyyymmddhhmmss (without any hyphens, dashes, colons, etc).

Has anyone any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝戈者 2024-09-25 16:05:13

您可以使用 strtotimedate 函数:

echo date('Ymdhis', strtotime($date));

You can use the strtotime and date function:

echo date('Ymdhis', strtotime($date));
与之呼应 2024-09-25 16:05:13

使用日期

$date = date("YmdHis", $timestamp);

Use date.

$date = date("YmdHis", $timestamp);
久随 2024-09-25 16:05:13

干得好

print date("YmdHis",unixtimestamp);

here you go

print date("YmdHis",unixtimestamp);
只为一人 2024-09-25 16:05:13

如果您使用 mysql,它有一个 DATE_FORMAT 函数。您可以这样称呼它

SELECT DATE_FORMAT(`datefield`, "%Y%m%d%H%i%s") AS `date_formatted` FROM table;

,因此您可以从数据库中获取已经格式化的日期。

If you're using mysql, it has a DATE_FORMAT function. You can call it like

SELECT DATE_FORMAT(`datefield`, "%Y%m%d%H%i%s") AS `date_formatted` FROM table;

So you get an already formatted date from your database.

小猫一只 2024-09-25 16:05:13

使用正确的格式:

date('YmdHis');

Use the right format :

date('YmdHis');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文