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

  • Home
  • SEARCH
  • 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 4118046
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:54:37+00:00 2026-05-20T22:54:37+00:00

One of the annoying things about php’s DateTime class is that it doesn’t have

  • 0

One of the annoying things about php’s DateTime class is that it doesn’t have a __toString() method, meaning that it throws an error every time you try to use it in a string context. So I extended it:

class ZDateTime extends DateTime {

    private $zformat='Y-m-d H:i:s';

    function __toString() {
        return $this->format($this->zformat);
    }
    
    function set_format($format){
        $this->zformat=$format; //leaving out format validation for brevity
    }
}

But now I’ve lost all the nice procedural functions that DateTime has like date_create() and many of the other functions listed here.

I know that the procedural functions are just aliases for DateTime class methods. But they are often easier to use and are liberally sprinkled throughout my code. Fishing them out will be a severe pain. So, any idea how I can make date_create() return a ZDateTime object?

  • 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-20T22:54:38+00:00Added an answer on May 20, 2026 at 10:54 pm

    The easiest way is probably just to create a zdate_create function and do a mass search & replace.

    It can be done, but the only way you can do that in vanilla PHP (i.e.: without runkit and such) is:

    ZDateTime.php

    namespace MyDateTime;
    
    class ZDateTime extends \DateTime {
        // do your stuff
    }
    
    function date_create($time = 'now', \DateTimeZone $timezone = null) {
        if (isset($timezone)) {
            return new ZDateTime($time, $timezone);
        } else {
            return new ZDateTime($time);
        }
    }
    

    Some_Script.php

    namespace MyDateTime;
    include 'ZDateTime.php';
    
    $foo = date_create();
    var_dump($foo); // ZDateTime object
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of the most annoying things about the new eclipse version is that it
One of the few annoying things about the Eclipse Java plug-in is the absence
I have a layout that is working, but it has one very annoying problem..
I have some questions about some things in Asp.net MVC that still confuses me.
One of my biggest typographical frustrations about HTML is the way that it mangles
I have a script that appends some rows to a table. One of the
This is one of those annoying things where you know the answer is easy,
This is one of those annoying cases where things are working but I don't
I have a situation in which we have two production databases that synchronize with
I normally work these things out reasonably quickly, but this one is a headache.

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.