wgp
wgp

Reputation: 1157

strtotime() always returns same day of month but gets other parts of date correct

I have a part of a simple web app that takes input from a JavaScript calendar picker, sends it to the server, and then the server converts it to a human readable time and echos it back out.

My HTML form ends up having a value formatted as MM/DD/YYYY.

When this gets POSTed to the server this PHP transforms it into a differet format (please note that I'm using CodeIgniter so $this->input->post() is the same as $_POST[]):

php $date = date('l, F n, Y', strtotime($this->input->post('date')));

Example input and output

HTML text input will get a value of "04/21/2013".

PHP's strtotime() will echo back "Sunday, April 4, 2013".

No matter what date I put in there, strtotime() always gives me the correct date back except for the day of the month which always ends up being the same number as the number of the month (for example, any dates in May become "May 5, 2013" and so on).

Update: Solved

As soon as I posted this I realized it was the 'n' in 'l, F n, Y' that caused the issue. Turning it to a 'j' fixed things. Sorry to waste everyone's time.

Upvotes: 1

Views: 500

Answers (2)

Keeleon
Keeleon

Reputation: 1422

See the documentation for date() here: http://php.net/manual/en/function.date.php

Change the n in the first parameter of your date function to j and you will get the number of the day of the month.

Upvotes: 2

John Conde
John Conde

Reputation: 219794

Use j for day of the month, not n which is the numeric month:

php $date = date('l, F j, Y', strtotime($this->input->post('date')));

See it in action

Upvotes: 4

Related Questions