SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    change date format

    I have a date in this format: October 6, 2009
    I need it in this format: YYYY-MM-DD

    This does what I need:
    Code PHP:
    // $theDate equals 'October 6, 2009'
    function formatDate($theDate){
    		$newDate = '';
    		$timestamp = strtotime($theDate);
    		$newDate .= date(Y, $timestamp).'-';
    		$newDate .= date(m, $timestamp).'-';
    		$newDate .= date(d, $timestamp);
    		return $newDate;
    	}

    Is there a way to make this function a bit tighter? Something like this, but this is returns 0:
    Code PHP:
    function formatDate($theDate){
    		$timestamp = strtotime($theDate);
    		return date(Y-m-d, $timestamp);
    	}

  2. #2
    SitePoint Member JAY6390's Avatar
    Join Date
    Oct 2009
    Location
    UK
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function formatDate($theDate){
         return 
    date('Y-m-d'strtotime($theDate));


  3. #3
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh i see, I didn't quote Y-m-d. Thanks.

  4. #4
    SitePoint Member JAY6390's Avatar
    Join Date
    Oct 2009
    Location
    UK
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep!

  5. #5
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively, you can make use of PHP's built-in DateTime class:

    PHP Code:
    $theDate 'October 6, 2009';
    $newDate = new DateTime($theDate); // new DateTime object named $newDate
    echo $newDate->format('Y-m-d'); // Output: 2009-10-06 


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
  •