Current location: Hot Scripts Forums » Programming Languages » PHP » Date format problem


Date format problem

Reply
  #1 (permalink)  
Old 12-12-07, 11:10 PM
LakkadBabu LakkadBabu is offline
Newbie Coder
 
Join Date: May 2004
Location: India
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Date format problem

I am excepting date from the user in a format "d/m/Y" (eg. "22/03/2007")
and want to submit in the format "Y/m/d" ("2007/03/22")
because Mysql store date in this format
Please help
__________________
Lakkad Babu
Trying To Code
Vinsar.Net - Your Web Host For Life Cyber Udyog - The Cyber Entrepreneurs
Reply With Quote
  #2 (permalink)  
Old 12-13-07, 12:56 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Like this:
PHP Code:

<?php
$date 
"22/03/2007";
$temp_date explode("/",$date);
$stamp mktime(0,0,0,$temp_date[1],$temp_date[0],$temp_date[2]);
$new_date date('Y/m/d',$stamp);
echo 
$new_date;
?>
__________________
Jerry Broughton

Last edited by job0107; 12-13-07 at 01:02 AM.
Reply With Quote
  #3 (permalink)  
Old 12-13-07, 01:12 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Here is a different method that also validates the format (but not the actual values) and is also not limited by the Unix timestamp min/max values -

PHP Code:

    if(preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) == 1)

    {
        
// This puts the d (or dd), m (or mm), and year in $parts[1] through $parts[3]
        // have a valid date, put into yyyy-mm-dd format
        
$mysql sprintf("%d-%02d-%02d",$parts[3],$parts[2],$parts[1]);
        echo 
"Before: $date, after: $mysql<br />";
    } else {
        echo 
"The date format: $date was invalid<br />";
    } 
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #4 (permalink)  
Old 12-13-07, 02:14 AM
LakkadBabu LakkadBabu is offline
Newbie Coder
 
Join Date: May 2004
Location: India
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
Like this:
PHP Code:

<?php

$date 
"22/03/2007";
$temp_date explode("/",$date);
$stamp mktime(0,0,0,$temp_date[1],$temp_date[0],$temp_date[2]);
$new_date date('Y/m/d',$stamp);
echo 
$new_date;
?>
Thanks I tried this code and it worked.
__________________
Lakkad Babu
Trying To Code
Vinsar.Net - Your Web Host For Life Cyber Udyog - The Cyber Entrepreneurs
Reply With Quote
  #5 (permalink)  
Old 12-13-07, 02:56 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
mab's code works real good to. And you can put it all on one line using boolean logic.
PHP Code:

<?php
$date 
"22/03/2007";
$new_date preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) ? sprintf("%d/%02d/%02d",$parts[3],$parts[2],$parts[1]) : "The date format: $date was invalid<br />";
echo 
$new_date;
?>
But there is one drawback, if you had a date like this "22/13/2007" (ie:d/m/Y), it would also validate
even though the month is set to 13.

And this date "22/00/2007" would also validate even though the month is 00.

However, it will not roll forward or backward to compensate.

But you can check for these erroneous values if you don't want it to roll backword or forward.

And mab's code isn't restricted to the starting and ending years that mktime() is.
__________________
Jerry Broughton

Last edited by job0107; 12-13-07 at 03:19 AM.
Reply With Quote
  #6 (permalink)  
Old 12-13-07, 09:05 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Version which adds date validation. No human enters dates expecting out of range rollover/rollback. Such values would indicate a typo was made and should not be processed further. -

PHP Code:

    if(preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) == 1)

    {
        
// This puts the d (or dd), m (or mm), and year $parts[1] through $parts[3]
        // echo "<pre>",print_r($parts),"</pre>";
        // have a valid date, put into yyyy-mm-dd format
        
$mysql sprintf("%d-%02d-%02d",$parts[3],$parts[2],$parts[1]);
        if(!
checkdate($parts[2], $parts[1], $parts[3]))
        {
            echo 
"The date: $mysql is invalid<br />";
        } else {
            echo 
"Before: $date, after: $mysql<br />";
        }
    } else {
        echo 
"The date format: $date is invalid<br />";
    } 
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #7 (permalink)  
Old 12-13-07, 02:05 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by mab View Post
Version which adds date validation. No human enters dates expecting out of range rollover/rollback. Such values would indicate a typo was made and should not be processed further. -

PHP Code:

    if(preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) == 1)
    {
        
// This puts the d (or dd), m (or mm), and year $parts[1] through $parts[3]
        // echo "<pre>",print_r($parts),"</pre>";
        // have a valid date, put into yyyy-mm-dd format
        
$mysql sprintf("%d-%02d-%02d",$parts[3],$parts[2],$parts[1]);
        if(!
checkdate($parts[2], $parts[1], $parts[3]))
        {
            echo 
"The date: $mysql is invalid<br />";
        } else {
            echo 
"Before: $date, after: $mysql<br />";
        }
    } else {
        echo 
"The date format: $date is invalid<br />";
    } 
Yes, that works good.

Here's the same thing in boolean logic.
PHP Code:

$mysql preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) ? checkdate($parts[2], $parts[1], $parts[3]) ? sprintf("%d/%02d/%02d",$parts[3],$parts[2],$parts[1]) : "The date format: $date is invalid<br />" "";
if(!
is_numeric($mysql[0])){echo $mysql."<br />";}
else
{
 
// Do something with $mysql. Like store it in a database.
 
echo $mysql;
 } 
Or you could check for the "T" in the error message:
PHP Code:

$mysql preg_match('@(\d{1,2})/(\d{1,2})/(\d{4})@',$date,$parts) ? checkdate($parts[2], $parts[1], $parts[3]) ? sprintf("%d/%02d/%02d",$parts[3],$parts[2],$parts[1]) : "The date format: $date is invalid<br />" "";
if(
$mysql[0] == "T"){echo $mysql."<br />";}
else
{
 
// Do something with $mysql. Like store it in a database.
 
echo $mysql;
 } 
__________________
Jerry Broughton

Last edited by job0107; 12-13-07 at 02:13 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
date format mysql and php Deansatch PHP 9 03-28-08 04:06 AM
converting date-string to Date object UnrealEd Everything Java 4 05-15-07 06:20 PM
Newbie found a solution to a date problem. mickey_kamer Perl 4 05-09-07 05:54 AM
php date problem eric PHP 0 09-14-05 10:13 PM
Problem with date and form djavet PHP 3 05-02-04 12:37 AM


All times are GMT -5. The time now is 07:33 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.