SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I convert this date?

    Hi,
    I have a date in this format, dd/mm/yyyy, 15/04/2008

    I want to enter this into a mysql DB as yyyy/mm/dd

    When i do this

    Code:
    $dcp = date( 'Y-m-d', $dcp );
    I get $dcp = 1970-01-01

    What do I need to do to get the format correct?

    thanks

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As far as I know, MySQL should accept an ISO 1801 formatted date...
    PHP Code:
    <?php
    echo date(
        
    'c',
        
    mktime(
            
    null,
            
    null,
            
    null,
            
    substr('15/04/2008'32),
            
    substr('15/04/2008'02),
            
    substr('15/04/2008'64)
        )
    ); 
    #2008-04-15T00:00:00+01:00
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whats the problem with the way I am currently doing it?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second argument to the date() function needs to be a unix timestamp(which must be an integer). Since you're sending a string, when php tries to convert it to an integer, the conversion of 15/04/2008 ends up being integer 15. A unix timestamp of 15, corresponds to the 15'th second in 1970.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    If the date parts in your string are separated by forward slash characters, the date will be interpreted as mm/dd/yyyy however if you use the dash character it will be dd-mm-yyyy (note the month/day order).

    For example, try:
    PHP Code:
    echo date('M j'strtotime('09/08/2009')), "\n"// Sep 8
    echo date('M j'strtotime('09-08-2009')), "\n"// Aug 9 
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •