PHP获取用户输入和格式化输出(日期)

PHP获取用户输入和格式化输出(日期),php,Php,我是php新手,需要一些帮助,我正在尝试获取用户输入并格式化它,以便输出显示一个月的最后一天。以下是我目前掌握的情况: <?php //form for user-input echo "<form name='form' method='post' action='array.php'>"; echo "<p>Enter Date in ' Month/Day/Year ' format<p>"; echo "<i

我是php新手,需要一些帮助,我正在尝试获取用户输入并格式化它,以便输出显示一个月的最后一天。以下是我目前掌握的情况:

<?php
    //form for user-input
    echo "<form name='form' method='post' action='array.php'>";
    echo "<p>Enter Date in ' Month/Day/Year ' format<p>";
    echo "<input type='text' id='date-input' name='date-input' placeholder='Enter date' />";
    echo "</form>";

    //grab user-input
    $input=$_POST["date-input"];

    //output in correct format
    echo $input->format("m/t/Y");


?>

第190行是我的回音输入行。

类似替换的内容

//output in correct format
    echo $input->format("m/t/Y");


试试这个。您可能需要首先从输入字符串创建日期。使用


注意:记住在所有日期时间函数中也要使用时区参数。

可能符合您的要求

<?php

$date = "2040-11-23";

$Year = DateTime::createFromFormat("Y-m-d", $date)->format("Y");

$YearWithDay = DateTime::createFromFormat("Y-m-d", $date)->format(" Y-m, t \d\a\y.");

function is_leap_year($year)
{
   return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
}

echo $YearWithDay."<br>";
echo (is_leap_year($Year))?"This Year is Leap year": "";

?>

$input
是字符串而不是对象。尝试使用
echo$input
如果需要日期,可能需要将字符串转换为日期。看看当你说“output shows the last day of the month”时,这是否意味着你希望它输出所提供的月份中的天数?基本上是的,这就是为什么我试图使用“t”来设置总天数的格式,我只想输出给定月份中用户输入的天数作为日期。因此,假设用户类型为2017年5月2日。我希望输出显示为2017年2月28日。我还希望它计算闰年。@bansi,我想尝试避免使用strotime,因为它在2038年后将不起作用。请尝试使用。但别忘了,否则你可能会得到意想不到的结果,只是看到了编辑,但我想尝试避免使用strotime,因为它在2038年后不起作用,这意味着你正在传递错误的输入。查看此示例并测试它
echo$input=date('dmy');回显“
”。日期(“m/d/Y”,标准时间($input))确保月份应该是字符串(例如,一月或一月),而不是数字。我使用了echo“echo
”。输入=日期(“m/t/Y”,strotime($input));它就像我想要的那样工作,但我想再次使用Strotime以外的另一种方法。这非常有效!现在我将介绍如何实现DateTimeZone和/或使用时区参数。非常感谢。
//output in correct format
    echo date("m/d/Y",strtotime($input));
$date = DateTime::createFromFormat('m/d/Y', $input);
echo $date->format('m/t/Y');
<?php

$date = "2040-11-23";

$Year = DateTime::createFromFormat("Y-m-d", $date)->format("Y");

$YearWithDay = DateTime::createFromFormat("Y-m-d", $date)->format(" Y-m, t \d\a\y.");

function is_leap_year($year)
{
   return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
}

echo $YearWithDay."<br>";
echo (is_leap_year($Year))?"This Year is Leap year": "";

?>
2040-11, 30 day.
This Year is Leap year