mm / dd / yyyy格式使用PHP时代 [英] mm/dd/yyyy format to epoch with PHP

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

问题描述

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



预先感谢。



其他信息:



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



表单ID为 date,数据库字段为 epoch



以下是我在尝试发布日期时要尝试的操作(失败):

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

我从上一篇文章中了解到,这仍然会以mm / dd / yyyy的形式提交值,而不是mm,dd,yyyy,如mktime预期的那样,但是该帖子未提供有关如何执行此操作的决议。我尝试使用str_replace将 /更改为,,并且得到了相同的结果-无论输入的日期如何,都获得了今天的纪元日期。



这里是代码示例-再次无法使用,但是我添加了它以帮助说明我已经尝试过的内容

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

也感谢先前的回复,我也不想让快速回复被忽略! / p>

感谢大家!



感谢大家的答复-strtotime似乎是在初始测试中效果最好,这可能是解决方法,因为这种格式在整个网站上都是一致的。但是所有建议都对其他一些事情有所帮助,所以再次感谢大家!

解决方案

如果您知道它将始终采用这种格式,strtotime会将其直接转换为unix时间戳。

  strtotime($ _ POST ['app_date']) ; 

HTH!


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?

Thanks in advance.

Additional information:

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

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

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

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

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!

Thanks everyone!

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!

解决方案

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!

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

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