Search code examples
functiondatephpdate-conversion

How to use PHP date() to convert from YYYY-MM-YY to "01 January 2011" in a function


I am able to convert the date (2011-01-05 to 05 January 2011) using:

<?php   
$whatever = get_post_meta($post->ID, start_date, true);
$nice_date = date("d F Y", strtotime($whatever));
echo $nice_date;
?>

However, I would like to implement that in a function so I can use it in different places:

<?php   

function newDate($whatever) {
$nice_date = date("d F Y", strtotime($whatever));
return $nice_date; }

$crap_date = get_post_meta($post->ID, start_date, true);
echo newDate($crap_date);

?>

The function is inside a while loop (WordPress). First date is getting formatted properly, but on the second I get the following error message:

Fatal error: Cannot redeclare newDate() (previously declared in..

How would I make this work and why is that happening? Thanks.


Solution

  • You have put the function definition itself inside a loop. For example:

    while ($someCondition) {
    
      function newDate () {
    
        // Function code
    
      }
    
      // Loop code
    
    }
    

    This attempts to redeclare the function on every iteration of the loop, which will result in the error your see.

    Either wrap the function definition in an if:

    while ($someCondition) {
    
      if (!function_exists('newDate')) {
        function newDate () { 
    
          // Function code
    
        }
      }
    
      // Loop code
    
    }
    

    Or (better) declare the function before the loop:

    function newDate () { 
    
      // Function code
    
    }
    
    while ($someCondition) {
    
      // Loop code
    
    }
    

    EDIT Following on from you comment below, here is how that code could be rewritten to use the DateTime object:

    function format_date ($dateStr, $formatStr = 'd F Y') {
      $date = new DateTime($dateStr);
      return $date->format($formatStr);
    }
    
    $crap_date = get_post_meta($post->ID, 'start_date', true);
    echo format_date($crap_date);
    

    This function accepts a string in any date format that can be parsed by the DateTime object as it's first argument (I think uses the same internal mechanism as strtotime()). The optional second argument is a format string identical to the first argument for the date() function - if this is omitted, the default d F Y will be used.

    Regarding your OOP questions:

    Is this approach better? - That is very much a matter of opinion. I see it commented here that the DateTime object is better than the strtotime()/date() approach and vice versa, but really what this comes down to is that you should use the approach you understand best, the one that makes the most sense for a given situation, and the one that makes you code most readable to you and other developers you may be working with. I have never seen a convincing argument for one being definitively better than the other. For the routine above, I don't think it makes much difference.

    How could I rewrite my function in that format? - See above.

    Is DateTime the object and format the method to change a property? - DateTime is the name of a class. In the example code above, the $date variable is an object, which is an instance of the DateTime class. And yes, format is the name of a method.

    Would this help me understand OO better if I will try and write all the code in this approach, where possible? - OOP requires a different way of thinking than writing procedural code, and it is not a trivial thing to pick up. There are many, many resources out there to help you get to grips with OOP so I won't get into it here, Google would be the place to start. The one thing I will say is that if you want to understand OOP, PHP is not the place to start. PHP is not an OO language, it is a scripting language that provides OO support. I would point you in the direction of Java for learning how to think in OO, although others can and will disagree.