Format A Raw Date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Format A Raw Date

Post by icesolid »

I have a field called 'due_date'. When the user types their date in that box I have five characters available for input. I am looking for mm-dd format to be inputted. However, the user doesn’t always submit it that way. How can I reformat that 'due_date' so into an mm-dd format no matter how it is submitted? I do not use the year in this.

I do not want to use a selection box for this because the range of dates that would be submitted here is very large. I do not want people to have to scroll through some large list of dates.
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Format A Raw Date

Post by anto91 »

Its rather simple and does not require PHP use a html select element

Example

Code: Select all

 <select name="month">   <option value="1">January</option>   <!-- and continue --></select><select name="day">   <option value="1">1</option>   <!-- and continue --></select> 
You might want to add a simple javascript to add 1 or remove one day depending on the month but i find this a simple solution that is always valid, unless the user creates there own form and sends directly to you :=)


Edit: oh sorry didnt read the lastbit >.< my fault


Solution 2:

use a regex expression and if its not valid create your own date of the current day

I would give an example but i dont know regex
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Format A Raw Date

Post by icesolid »

How can I determine the number of days in a month and then print them all out including the traling zeros.

Example: It woukld determine it is March, so it would print out 01-31
Last edited by icesolid on Tue Mar 11, 2008 10:14 am, edited 2 times in total.
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Format A Raw Date

Post by anto91 »

there is probaly a better way but you could use this

Code: Select all

 
<?php
$day = 2;
 
if($day <= 9) {
$day = '0'.$day;
}
?>
 
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Format A Raw Date

Post by icesolid »

Sorry that is not what I am looking for.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Format A Raw Date

Post by pickle »

If you don't mind requiring javascript, use a Javascript date picker.

Or have 2 different fields - one for the month & one for the day. It'd be pretty easy to check that both of them are integers. As to whether it's a valid day of the month, date() has the ability to check how many days are in a given month (but you must provide a year) - which raises another question. What are you going to do about Feb 29?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Format A Raw Date

Post by icesolid »

I have solved that problem. Now I am wondering how to do this:

How can I determine the number of days in a month?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Format A Raw Date

Post by pickle »

I believe that's already been covered:
pickle wrote:As to whether it's a valid day of the month, date() has the ability to check how many days are in a given month (but you must provide a year)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Format A Raw Date

Post by icesolid »

OK I want to know how many days are in the month of March 2008.

I don't see where he covered that. He talked about if I wasn’t using the year, but I have solved that problem, I am using the year now. So I will know that it is 2008 or 2009 or whatever the current year is.
Post Reply