在 PHP 中将 TIMESTAMP 转换为 unix 时间?

发布于 2024-09-02 12:03:51 字数 187 浏览 2 评论 0原文

目前我将时间存储在数据库中,如下所示: 2010-05-17 19:13:37

但是,我需要比较两次,我觉得如果它是一个会更容易unix 时间戳,例如 1274119041。 (这两个时间是不同的)

那么如何将时间戳转换为unix时间戳呢?有没有一个简单的php函数可以实现?

Currently I store the time in my database like so: 2010-05-17 19:13:37

However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041. (These two times are different)

So how could I convert the timestamp to unix timestamp? Is there a simple php function for it?

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

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

发布评论

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

评论(5

萌能量女王 2024-09-09 12:03:51

您正在寻找 strtotime()

You're looking for strtotime()

蝶…霜飞 2024-09-09 12:03:51

您想要 strtotime

print strtotime('2010-05-17 19:13:37'); // => 1274123617

You want strtotime:

print strtotime('2010-05-17 19:13:37'); // => 1274123617
故乡的云 2024-09-09 12:03:51

获取 unixtimestamp:

$unixTimestamp = time();

转换为 mysql 日期时间格式:

$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);

获取一些 mysql 时间戳:

$mysqlTimestamp = '2013-01-10 12:13:37';

将其转换为 unixtimestamp:

$unixTimestamp = strtotime('2010-05-17 19:13:37');

...将其与一个或一系列时间进行比较,以查看用户是否输入了实际时间:

if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}

Unix 时间戳也更安全。在检查(例如)之前的范围检查之前,您可以执行以下操作来检查 url 传递的变量是否有效:

if(ctype_digit($_GET["UpdateTimestamp"]))
{...}

Getting a unixtimestamp:

$unixTimestamp = time();

Converting to mysql datetime format:

$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);

Getting some mysql timestamp:

$mysqlTimestamp = '2013-01-10 12:13:37';

Converting it to a unixtimestamp:

$unixTimestamp = strtotime('2010-05-17 19:13:37');

...comparing it with one or a range of times, to see if the user entered a realistic time:

if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}

Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:

if(ctype_digit($_GET["UpdateTimestamp"]))
{...}
回忆躺在深渊里 2024-09-09 12:03:51

如果您使用 MySQL 作为数据库,它可以使用 UNIX_TIMESTAMP

SELECT UNIX_TIMESTAMP(my_datetime_field)

您也可以在 PHP 端使用 strtotime

strtotime('2010-05-17 19:13:37');

If you're using MySQL as your database, it can return date fields as unix timestamps with UNIX_TIMESTAMP:

SELECT UNIX_TIMESTAMP(my_datetime_field)

You can also do it on the PHP side with strtotime:

strtotime('2010-05-17 19:13:37');
空城缀染半城烟沙 2024-09-09 12:03:51

如果你将时间存储在数据库中,为什么不让数据库也给你它的unix时间戳呢?请参阅 UNIX_TIMESTAMP(日期),例如。

SELECT UNIX_TIMESTAMP(date) ...;

数据库还可以进行日期和时间比较和算术。

if you store the time in the database, why don't you let the database also give you the unix timestamp of it? see UNIX_TIMESTAMP(date), eg.

SELECT UNIX_TIMESTAMP(date) ...;

databases can also do date and time comparisons and arithmetic.

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