Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将日期和月份与给定日期分开_Javascript_Php_Jquery - Fatal编程技术网

Javascript 将日期和月份与给定日期分开

Javascript 将日期和月份与给定日期分开,javascript,php,jquery,Javascript,Php,Jquery,我已经创建了一个文本框,可以在其中绑定日期和年份。我就是这样做的: jQuery("#year_month").datetimepicker({ format: 'YYYY-MM' }); 以下是我在php中的接收方式: $year_month = mysqli_real_escape_string($mysqli, $_REQUEST["year_month"]); 在这里我必须把年和月分开。我如何才能做到这一点?尝试使用explode()函数: <?php $example_st

我已经创建了一个文本框,可以在其中绑定日期和年份。我就是这样做的:

jQuery("#year_month").datetimepicker({ format: 'YYYY-MM' });
以下是我在php中的接收方式:

$year_month = mysqli_real_escape_string($mysqli, $_REQUEST["year_month"]);
在这里我必须把年和月分开。我如何才能做到这一点?

尝试使用
explode()
函数:

<?php

$example_string = "2016-05-12 16:27:11: 2016-05";

list($year, $month) = explode("-", substr($example_string, 0, 7));

echo $year . "\n";
echo $month;

您可以使用PHP函数从日期字符串中提取年份和月份

$dateString = "2016-05-12 16:27:11"; // = mysqli_real_escape_string($mysqli, $_REQUEST["year_month"]);

$year = date('Y', strtotime($dateString));
$month= date('m', strtotime($dateString));

echo "$year-$month";

>> 2016-05

如果要用JS或PHP?分隔年和月,请显示变量
$\u请求[“年/月”]
2016-05-12 16:27:11:2016-05的示例。这是我收到的年份和月份。现在我得把它们分开。这就是我想要的。
$dateString = "2016-05-12 16:27:11"; // = mysqli_real_escape_string($mysqli, $_REQUEST["year_month"]);

$year = date('Y', strtotime($dateString));
$month= date('m', strtotime($dateString));

echo "$year-$month";

>> 2016-05