lional Posted June 9, 2006 Share Posted June 9, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/11559-formatting-date/ Share on other sites More sharing options...
wildteen88 Posted June 9, 2006 Share Posted June 9, 2006 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"; - invalidif(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 Link to comment https://forums.phpfreaks.com/topic/11559-formatting-date/#findComment-43562 Share on other sites More sharing options...
localhost Posted June 9, 2006 Share Posted June 9, 2006 echo date('d/m/Y'); Quote Link to comment https://forums.phpfreaks.com/topic/11559-formatting-date/#findComment-43565 Share on other sites More sharing options...
joquius Posted June 9, 2006 Share Posted June 9, 2006 [!--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"; - invalidif(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] Quote Link to comment https://forums.phpfreaks.com/topic/11559-formatting-date/#findComment-43578 Share on other sites More sharing options...
poirot Posted June 9, 2006 Share Posted June 9, 2006 Or you can use the pretty built-in checkdate() function.[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]checkdateValidate 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] Quote Link to comment https://forums.phpfreaks.com/topic/11559-formatting-date/#findComment-43698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.