php strtotime() 一些帮助

发布于 2024-09-01 13:13:38 字数 142 浏览 7 评论 0原文

我正在获取信用卡详细信息,并在两个表单字段中获取到期日期,一个用于到期月份,一个用于到期年份,我想将到期日期存储为时间戳。 strtotime("05/2010") 会创建时间戳吗?还是我还需要度过一天,或者有其他选择吗?

谢谢

I am taking credit card details and I am taking the expiration date in two form field, one for the expiration month and one for the expiration year, I am wanting to store the expiration date as timestamp. Will strtotime("05/2010") create a time stamp or do I need to pass a day as well or is there an alternative?

Thanks

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

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

发布评论

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

评论(3

红尘作伴 2024-09-08 13:13:38

不,那是行不通的,strtotime("05/2010") 将返回 false。您还需要指定一天。但是,这种情况存在歧义,因为 strtotime("01/05/2010") 是指 2010 年 5 月 1 日还是 2010 年 1 月 5 日?等

你可能最好使用 mktime() 而不是 strtotime (),因为您可以明确指出哪些部分是月份和年份,然后只需传递 1 作为日期。

$timestamp = mktime(0,0,0,$month,1,$year); // Where $month = 5, $year = 2010

No, that won't work, strtotime("05/2010") will return false. You would need to specify a day as well. However, there's ambiguity in that case as does strtotime("01/05/2010") mean the 1st of May 2010, or the 5th of January 2010? etc

You're probably better using mktime() instead of strtotime(), since you can explicitly state which parts are the month and year, and then just pass 1 as the day.

$timestamp = mktime(0,0,0,$month,1,$year); // Where $month = 5, $year = 2010
番薯 2024-09-08 13:13:38

更好地使用 mktime():
mktime(0, 0, 0, $month, 1, $year)

这将生成给定年份中给定月份第一天的时间戳,时间为 00:00:00。

Better use mktime():
mktime(0, 0, 0, $month, 1, $year)

This will generate a timestamp for the 1st day of the given month in the given year with the time 00:00:00.

北凤男飞 2024-09-08 13:13:38

如果您传递“1”作为日期,您的编程将导致信用卡在该月的第一天到期。这不是标准做法,即该卡在该月的最后一天到期。可以使用 PHP date('t') 找到该月的最后一天。

也许您会看到这篇文章(希望如此)。它教授了有关在 PHP 中处理 DATETIME 值所需了解的大部分内容。

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

最好的问候,~Ray

If you pass "1" as the day, your programming will cause the credit card to expire on the first of the month. This is not the standard practice, which is to have the card expire on the last day of the month. The last of the month can be found with PHP date('t').

Maybe this article will be visible to you (hope so). It teaches most of what you need to know about handling DATETIME values in PHP.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray

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