Jump to content

Converting to a date format


jmahdi

Recommended Posts

Hi there,

 

i want to convert whatever a user inputs through and html form to a DATE format which when posted will be entered (using mysql) as that particular Date format , i'm using this:

 

$date = "12-12-11"; //assume this to be the user input

	$time = strtotime($date);
	$new_date = date("d/m/Y",$time);
                echo $new_date;

 

but this format will give the output as:  11/12/2012.....is there a way to convert whatever the user enters to be: dd/mm/yyyy which gives: 12/12/2011

thanks so much

 

Link to comment
Share on other sites

$date = "10-12-11"; //assume this to be the user input

What date is that?  Is that October 12, 2011?  Is it December 10, 2011?  Is it December 11, 2010?  All three of those dates are valid interpretations of that string.  That's your problem: Your user input is ambiguous.

 

You need to attempt to use explode() and mktime() to make a valid timestamp out of these dates, then you can use date() to format it however you like.

 

-Dan

Link to comment
Share on other sites

You didn't reply to my message, which shows you exactly why you need to enforce a certain format from the users.

 

how about this for a converter:

$signs = array("-", "\\", "/", "*");
	$rpl = str_replace($signs, ' ', $date);
	$d = explode(" ", $rpl);
	$date = mktime(0,0,0,$d[0], $d[1], $d[2]);
	echo $date;
	echo strftime("%d/%m/%Y", $date);

I think this solves it....thanks again for the tip

Link to comment
Share on other sites

That assumes you know what order the user is entering the date in.  If you're 100% confident that they'll always use that order (and they're not, say, european or a smartass like me) then you're fine.

 

in fact...i give up :( ... how on earth can i guess in what order the user is inputting the date? help please....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.