使用 PHP 将 mm/dd/yyyy 格式转换为纪元 [英] mm/dd/yyyy format to epoch with PHP

查看:22
本文介绍了使用 PHP 将 mm/dd/yyyy 格式转换为纪元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 mysql 表,它依赖于相当于条目日期的 unix 纪元时间戳在网站的各个部分进行排序和过滤.我正在尝试实现一个日期选择器,它将日期以 mm/dd/yyyy 格式输入到表单字段中.我一直在努力将该日期转换为 unix 纪元格式以将该条目添加到行字段中.我所做的所有尝试都导致生成当天的纪元时间戳.有谁知道我如何采用该日期格式并将其转换为等效的纪元时间戳?

I have a mysql table that relies on the unix epoch time stamp equivalent of the date of the entry to sort and filter in various parts of the website. I'm trying to implement a date picker that will enter the date into the form field in the mm/dd/yyyy format. I've been struggling with converting that date into the unix epoch format to add that entry in the row field. All the attempts I've made have resulted in generating the current day epoch time stamp. Does anyone have any idea how I can take that date format and convert in the it's equivalent epoch time stamp?

提前致谢.

其他信息:

我一直在尝试 mktime,我得到的只是今天的时代.抱歉,我应该早点添加一些代码来更好地解释:

I have been trying mktime, and all I get is todays epoch. Apologies, I should have added some code earlier to better explain:

表单id是date",数据库字段是epoch"

The form id is "date" The database field is "epoch"

这是我在发布 for 日期时正在尝试的(未成功):

Here's what I'm trying (unsuccessfully) when I post the for date:

$epochhold = ($_POST['date']);
$epoch = mktime(0,0,0,$epochhold);

我从之前的帖子中了解到,这仍然会将值提交为 mm/dd/yyyy,而不是 mktime 期望的 mm、dd、yyyy,但是该帖子没有提供有关如何执行此操作的解决方案.我尝试使用 str_replace 将/"更改为",结果相同 - 无论输入的日期如何,都获取今天的纪元日期.

I understand from a previous post that this would still submit the value as mm/dd/yyyy and not mm, dd, yyyy as mktime expects, however the post didn't offer and resolution on how to do that. I tried a str_replace to change the "/" to "," and that yeilded the same result - getting today's epoch date regardless of the date entered.

这是那个代码示例 - 这同样不起作用,但我添加它以帮助说明我尝试过的内容

Here's that code example - again this didn't work, but I add it to help illustrate what I've tried

$epochhold = ($_POST['date']);
$epochold2 = str_replace('/', ', ', $epochhold)
$epoch = mktime(0,0,0,$epochhold2);

也感谢之前的回复,我不希望快速回复被忽视!

Thanks for the previous response as well, I didn't want the quick reply to go unnoticed!

谢谢大家!

感谢大家的回复 - strtotime 似乎在初始测试中工作得最好,并且可能是要走的路,因为这种格式在整个网站上都是一致的.但是所有的建议都对其他一些事情有所帮助,所以再次感谢大家!

Thanks to everyone for the replies - strtotime seems to be working best on the initial tests and may be the way to go as this format is consistent throughout the site. But all the suggestions helped with this a few other things all well so thank you all again!

推荐答案

如果你知道它永远是那个格式,strtotime 会直接把它转换成unix时间戳.

If you know that it will always be in that format, strtotime will convert it directly into the unix timestamp.

strtotime($_POST['app_date']);

HTH!

这篇关于使用 PHP 将 mm/dd/yyyy 格式转换为纪元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆