1

I'm new to php and need some help, I am trying to take the user input and format it so that the output shows the last day of the month. Here's what I have so far:

<?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");


?>

When I have just echo input variable without the format function and date format then it displays what the user inputs, but how it's set right now; nothing displays and I get this line:

Fatal error: Call to a member function format() on string in C:\xampp\htdocs\php-sessions\session-3\array.php on line 190

Line 190 is my echo input line.

6
  • $input is a string not an object. try using echo $input; and if you want date you may need to convert the string to date. Take a look at strtotime Commented Dec 14, 2016 at 4:08
  • When you say "output shows the last day of the month" does that mean you want it to output how many days are in the month provided? Commented Dec 14, 2016 at 4:11
  • Yes basically, which is why im trying to format using "t" for total days, I just want to output the number of days in the given month for what the user inputs as a date. So say the user types 2/05/2017. I want the output to display 2/28/2017 I also want it to count for leap years. Commented Dec 14, 2016 at 4:14
  • @bansi, I want to try an avoid strtotime due to the fact it will not work after year 2038 Commented Dec 14, 2016 at 4:16
  • try using DateTime. But don't forget DateTimeZone or you may get unexpected result Commented Dec 14, 2016 at 4:17

3 Answers 3

2

Try this. You may need to create a date first from your input string. Used createFromFormat

$date = DateTime::createFromFormat('m/d/Y', $input);
echo $date->format('m/t/Y');

Note: Remember to use timezone parameter also with all datetime functions.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly! I will look up now how to implement DateTimeZone and or use parameters for timezone. Thank you!
0

something like replace

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

with

//output in correct format
    echo date("m/d/Y",strtotime($input));

4 Comments

Just saw the edit, but I want to try and avoid strtotime due to it not working after year 2038
which means you are passing bad input. look into this sample and test it echo $input=date('d M Y'); echo "<br />".date("m/d/Y",strtotime($input));
Make sure month should be string (ex, Jan or January) not numeric.
I used echo "echo <br />".input=date("m/t/Y",strtotime($input)); and it worked just like I want, but again I would like to use another method other than strtotime
0

Maybe Match your requirements,

<?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": "";

?>

OUTPUT

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.