将 Javascript 日期格式转换为所需的 PHP 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3005944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a Javascript Date format to desired PHP format
提问by Aadi
How can we convert Wed Jun 09 2010 which is returns from a javascript function and get in a php script.I need to convert this date format to 2010-06-09.Thanks
我们如何转换 Wed Jun 09 2010,它是从 javascript 函数返回并进入 php 脚本。我需要将此日期格式转换为 2010-06-09。谢谢
回答by MANCHUCK
<?php
$jsDateTS = strtotime($jsDate);
if ($jsDateTS !== false)
date('Y-m-d', $jsDateTS );
else
// .. date format invalid
回答by Gordon
Or with DateTime:
或使用DateTime:
$date = new DateTime('Wed Jun 09 2010');
echo $date->format('Y-m-d');
The date format you can input to strtotime(), DateTimeand date_create()are explained in the PHP manual. If you need more control over the input format and have PHP5.3, you can use:
日期格式,您可以输入strtotime(),DateTime并且date_create()都在PHP手册中的说明。如果您需要更多地控制输入格式并拥有 PHP5.3,则可以使用:
$date = DateTime::createFromFormat('D M m Y', 'Wed Jun 09 2010');
echo $date->format('Y-m-d');
回答by Sarfraz
Either you can send the javascript date to php via query string:
您可以通过查询字符串将 javascript 日期发送到 php:
var mydate = encodeURIComponent('Wed Jun 09 2010');
document.location.href = 'page.php?date=' + mydate;
PHP
PHP
echo date('Y-m-d', strtotime(urldecode($_GET['date'])));
Or though a hidden field:
或者通过一个隐藏字段:
echo date('Y-m-d', strtotime(urldecode($_POST['date'])));

