0

Got some questions about the php date function.... Here it goes..

I use jquery's datepicker to get the date from the user in the format "day/month/Year". Eg. 23rd of november 2009 is 23/11/2009.

And to manipulate this is php i use the following code...

I store the date string in a variable $date

 $dateOf =  date ('(d-m-Y)', strtotime($date));

The problem i get is that when i pass the date string to the strtotime function it takes the number representing the month as the number representing the date.

Eg : When i give date as 24/12/2009 strtotime takes it as 12/24/2009

I can't get what i am doing wrong here... Will be glad if someone clears this up....

Thanks and Regards

3 Answers 3

1

On the PHP side, you can fix by doing this:

$date = "24/12/2009";
$date = preg_replace('/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/','\2/\1/\3', $date);

$dateOf =  date ('(d-m-Y)', strtotime($date));

That just swaps the position of the first and second match taking 24/12/2009 and making it 12/24/2009

Edit:

Without regex :

$date = "24/12/2009";
$date = explode('/', $date);
$date = implode('/', array($date[1], $date[0], $date[2]));

$dateOf =  date ('(d-m-Y)', strtotime($date));
Sign up to request clarification or add additional context in comments.

3 Comments

Cool work around... :-) Is there anyway to do it without Regex...?
I updated my answer to show an alternative. Still seems like a workaround, huh?
Both are good... Got nothing against Regex...!! I've used this explode technique before, but i thought php, being cool as it is, will have this included in one of the date functions saving us the extra code...!! Thanks all, for your valuable time and suggestions...
1

From the documentation:

The function expects to be given a string containing a US English date format

In the U.S., the month must come before the date, e.g. mm/dd/yyyy

Here's a workaround without Regex:

$date = explode('/', $date);
$dateOf = date('(d-m-Y)', strtotime($date[1].'/'.$date[0].'/'.$date[2]));

1 Comment

Sorry it "is" a function, but its depreciated, and if you aren't using regular expressions, explode is the preferred method.
0

strtotime parse an english date Here's an example showing how to do it:

<html>
<head>
<title>Php Date Function</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.ui.all.packed.js"></script>
<link rel="stylesheet" src="themes/default/ui.all.css"/>
<script type="text/javascript">
jQuery(document).ready(function($) {
    $('#dpicker').datepicker({
        dateFormat: "dd/mm/yy"
    });
})
</script>
</head>
<body>
    <form action="#" method="POST">
        <input size="25" readonly="readonly" type="text" value="" id="dpicker" name="dpicker" />
        <br/>
        <input type="submit" id="validBtn" value="OK" name="valid" />
    </form>
    <div>
    <?php 
        $d = (isset($_POST['dpicker']) ? $_POST['dpicker'] : null);
        if ($d) {
            $ds = explode("/", $d);
            $dte = date('(d-m-Y)', mktime(0, 0, 0, $ds[1], $ds[0], $ds[2]));
            echo '<p>Date: ' . $dte . '</p>';
        }
    ?>
    </div>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.