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.
You have put the function definition itself inside a loop. For example:
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:Or (better) declare the function before the loop:
EDIT Following on from you comment below, here is how that code could be rewritten to use the
DateTimeobject:This function accepts a string in any date format that can be parsed by the
DateTimeobject as it’s first argument (I think uses the same internal mechanism asstrtotime()). The optional second argument is a format string identical to the first argument for thedate()function – if this is omitted, the defaultd F Ywill be used.Regarding your OOP questions:
Is this approach better?– That is very much a matter of opinion. I see it commented here that theDateTimeobject is better than thestrtotime()/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?–DateTimeis the name of a class. In the example code above, the$datevariable is an object, which is an instance of theDateTimeclass. And yes,formatis 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.