Jump to content

Formatting date


lional

Recommended Posts

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
Link to comment
Share on other sites

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:
[code]<?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";
}

?>[/code]
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.
Link to comment
Share on other sites

[!--quoteo(post=381774:date=Jun 9 2006, 09:15 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 9 2006, 09:15 AM) [snapback]381774[/snapback][/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:
[code]<?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";
}

?>[/code]
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

[code]
<?
$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";
}
?>
[/code]
Link to comment
Share on other sites

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]
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.