Jump to content


Photo

Formatting date


  • Please log in to reply
4 replies to this topic

#1 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 09 June 2006 - 06:23 AM

I am trying to format the date from a variable. I have created a variable called $date wich is populated from a form. I want to make sure that the format will be correct because this is dependent on how the user inputs it. I want to format it dd/mm/yyyy. I know how to do it with the date function but does that not take the current date and not the date stored in the variable.
Any help will be appreciated

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 June 2006 - 08:15 AM

To check the format of the data you might want to use preg_match which will check whether the date is in the correct format, heres a basic example:
<?php

$date = "02/01/2006"; //valid
//$date = "02/13/2006"; - invalid

if(preg_match("/([0-9]{2})\/([0-12]{2})\/([0-9]{4})/", $date, $matches))
{
    echo "Valid";

    echo "<pre>" . print_r($matches, true) . "</pre>";
}
else
{
    echo "not valid";
}

?>
This code ONLY checks the format and not whether the date is valid, such as 31/02/2004 isnt a valid date but the format is correct. If you weant to check whether the data is valiud it'll require more tweeking.

#3 localhost

localhost
  • Members
  • PipPipPip
  • Advanced Member
  • 152 posts

Posted 09 June 2006 - 08:29 AM

echo date('d/m/Y');

#4 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 09 June 2006 - 11:00 AM

[!--quoteo(post=381774:date=Jun 9 2006, 09:15 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 9 2006, 09:15 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
To check the format of the data you might want to use preg_match which will check whether the date is in the correct format, heres a basic example:
<?php

$date = "02/01/2006"; //valid
//$date = "02/13/2006"; - invalid

if(preg_match("/([0-9]{2})\/([0-12]{2})\/([0-9]{4})/", $date, $matches))
{
    echo "Valid";

    echo "<pre>" . print_r($matches, true) . "</pre>";
}
else
{
    echo "not valid";
}

?>
This code ONLY checks the format and not whether the date is valid, such as 31/02/2004 isnt a valid date but the format is correct. If you weant to check whether the data is valiud it'll require more tweeking.
[/quote]
This will actually not work because 12 is not a charactar in regular expressions. [0-12] is taken as "0-1" and "2". any date with a month including any number beyond these will be invalid.

probably the best way is to input the months and explode the $date

<?
$month[1] = 31;
$month[2] = 28;
$month[3] = 31;
$month[4] = 30;
$month[5] = 31;
$month[6] = 30;
$month[7] = 31;
$month[8] = 31;
$month[9] = 30;
$month[10] = 31;
$month[11] = 30;
$month[12] = 31;

$verify = explode("/", $date);

if (is_numeric ($verify[0]) && $verify[0] <= $month[$verify[1]] && strlen ($verify[2]) <= 4)
{
echo "This date is valid";
}
?>

z..z..z..z..z..z..z..z..

#5 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 09 June 2006 - 04:03 PM

Or you can use the pretty built-in checkdate() function.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]checkdate
Validate a Gregorian date (PHP 3, PHP 4 , PHP 5)

bool checkdate ( int month, int day, int year )

Returns TRUE if the date given is valid; otherwise returns FALSE. Checks the validity of the date formed by the arguments. A date is considered valid if:

year is between 1 and 32767 inclusive
month is between 1 and 12 inclusive

Day is within the allowed number of days for the given month. Leap years are taken into consideration. [/quote]
[a href=\"http://www.php.net/checkdate\" target=\"_blank\"]http://www.php.net/checkdate[/a]
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users