如何转换“Day,dd Month yyyy”到“yyyy-mm-dd”在php [英] How to convert "Day, dd Month yyyy" to "yyyy-mm-dd" in php

查看:146
本文介绍了如何转换“Day,dd Month yyyy”到“yyyy-mm-dd”在php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换:

  2014年8月14日,星期四
/ pre>

到php中:

  2014-08-我尝试过strtotime(),trim()等,并且意识到不是这样的。你可以指导我吗?

解决方案

如果你面临将人的可读日期转换为数值的问题你必须重新考虑你的设计。您的代码只能使用32或64位元时间值(1970年1月1日以来的秒),然后将其转换为相应的格式。



无论如何,只要这样做:

  $ time = strtotime(stripslashes(2014年8月14日,星期四)); 
echo date(Y-m-d,$ time);记住要删除斜杠和逗号,因为它们可能会混淆strtotime


$



b $ b

我会避免这样的解决方案,因为它不是便携式的。您会发现这样的解决方案不起作用的PHP版本,而且输入字符串稍有不同可能会使strtotime失败,并且最重要的是 - > locale重要! (即,如果字符串是意大利语,则该字符串如何?)



只需使用历元值并进行转换即可将其显示给最终用户! :-)


How can I convert this:

Thursday, 14 August 2014

to this in php:

2014-08-14

I tried strtotime(), trim(), etc.. and realised thats not it. Can you guide me on this one please?

解决方案

if you're facing the problem of converting a human readable date to numeric values you have to reconsider your design. Your code should work only with 32 or 64 bit epoch values (seconds since 1st January 1970) and then convert them into the appropriate format only when needed.

In any case, just do this:

$time = strtotime(stripslashes("Thursday, 14 August 2014"));
echo date("Y-m-d",$time);

remember to strip out slashes and commas because they can confuse strtotime

I would avoid such a solution because it's NOT portable. You'll find PHP versions in which such solution doesn't work, moreover slightly different input strings could make strtotime to fail and most of all -> locale matters!!!! (i.e. what if the string is in italian language???)

Just work with epoch values and make a conversion only to show them to the end user! :-)

这篇关于如何转换“Day,dd Month yyyy”到“yyyy-mm-dd”在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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