Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:52:42+00:00 2026-05-31T00:52:42+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:52:43+00:00Added an answer on May 31, 2026 at 12:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to convert a date in french format (08 novembre 2011 à 06h00)
I am having a date format 16-11-2011. but I want to convert it into
I can't seem to be able to convert a string that contains date value
My problem is that i am not able to convert the US date value
Using https://github.com/AndreyNikiforov/vault2git I was able to convert a Vault repo to Git and then
I have the following date/time: 2011-09-27 13:42:16 I need to convert it to: 9/27/2011
I'm trying to convert following text to Date but not able to get it
I want to be able to convert between the Gregorian and Julian calendars (note:
I need to be able to convert dynamic HTML (html that is rendered on
I need to be able to convert from a Delphi Real48 to C# double.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.