Viewed   90 times

I've found a few problems in applications I've been doing where methods like DateTime::diff would be of much assistance, but my host with PHP 5.2 does not support DateTime::diff or any other from 5.3.

Can anyone point to a class implementation similar to PHP's DateTime class from v5.3, but working for PHP 5.2?

Thanks in advance



I use following wrapper class in my php5.2 apps: Untill php5.3 was released - it saves much my time

Thursday, November 3, 2022

I found this to work, yet there are some inconsistencies in PHP's DateTime class.

If the departing date is a sunday the previous monday is not considered the same week (fixed by this class). But departing from a monday, the next sunday is considered as the same week. If they fix that in the future this class will need some additions.

class EuroDateTime extends DateTime {

// Override "modify()"
public function modify($string) {

    // Change the modifier string if needed
    if ( $this->format('N') == 7 ) { // It's Sunday and we're calculating a day using relative weeks
        $matches = array();
        $pattern = '/this week|next week|previous week|last week/i';
        if ( preg_match( $pattern, $string, $matches )) {
            $string = str_replace($matches[0], '-7 days '.$matches[0], $string);
    return parent::modify($string);


Thursday, December 22, 2022

No need to format numbers with format() method. You can simply access public properties on DateInterval object:

$months = $interval->m + $interval->y * 12;


Monday, November 21, 2022

You could include different scripts based on version, then the script with syntax that isn't valid in 5.2 would never be included for that version.

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
} else {
Sunday, October 9, 2022

You need to output the JSON (not just return it), and let the client know that the content is JSON by setting the Content-Type:

// The user does not exist
if ( ! Gain::verifyIfUserExistByIdm($aOutput['UserId'])) {

    // If the user does not exist then "Forbidden" would make sense
    header("HTTP/1.0 403 Forbidden");

    // Let the client know that the output is JSON
    header('Content-Type: application/json');

    // Output the JSON
    echo json_encode(array(
        'ErrorCode'    => 407,
        'ErrorMessage' => 'Error',
    // Always terminate the script as soon as possible
    // when setting error headers
Wednesday, October 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :