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 488719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:44:07+00:00 2026-05-13T01:44:07+00:00

Does someone know a way to get a string from a date that contains

  • 0

Does someone know a way to get a string from a date that contains the format of the date?

<?php
    $date = date ("2009-10-16 21:30:45");

    // smething like this?
    print date_format ($date);
?>

I ask this because I’d like to optimize this function I’ve written, usually to get the date with a different timezone from a server, without doing particular things

<?php
function get_timezone_offset ($timezone, $date = null, $format = null, $offset_timezone = null) {
    if ($date == null) $date = date ($format);
    if ($offset_timezone == null) $offset_timezone = date_default_timezone_get ();
    if ($format == null) $format = "Y-m-d H:i:s";
    // I'd like to find a way that can avoid me to write $format and get it directly from the date i pass, but I don't know a particular method can do it
    // if ($format == null) $format = date_format ($date);

    $date_time = new DateTime ($date, new DateTimeZone ($offset_timezone));
    $date_time->setTimeZone (new DateTimeZone ($timezone));
    return $date_time->format ($format);
}

print get_timezone_offset ("Europe/Rome");
print get_timezone_offset ("Europe/Rome", date ("Y-m-d H:i:s"));
print get_timezone_offset ("Europe/Rome", date ("Y-m-d H:i:s"), "Y-m-d H:i:s");
print get_timezone_offset ("Europe/Rome", "2009-10-16 21:30:45", "Y-m-d H:i:s", "America/New_York");
?>

I hope to avoid regular expressions for performance reasons, but I don’t know if this is possible

  • 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-13T01:44:08+00:00Added an answer on May 13, 2026 at 1:44 am

    As far as I’m aware, there’s no guaranteed way of working backwards. The best way might be to try to match regular expressions against expected well-known formats (e.g. \d{2,4}[-/]\d{2}[-/]\d{2} for “Y-m-d”) but I can’t think of an easy way to do the matching without using regular expressions. You would also have to check that the parsed format makes sense, and you can’t do much about ambiguous dates like 2nd of March 2009, which could be represented as 09/03/02, 2009-03-02, 02/03/09, 03/02/09, or even 09/02/03.

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

Sidebar

Ask A Question

Stats

  • Questions 294k
  • Answers 294k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This ought to do the job (as edited to reflect… May 13, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer They use proprietary ones. Here is an article about a… May 13, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer Try something like this select distinct location From cross_reference_table where… May 13, 2026 at 6:41 pm

Related Questions

I'm trying to send a SOAP request using SOAPpy as the client. I've found
Does someone knows if it's possible to dynamically create a call chain and invoke
Given a connection object (or series of them), does anyone know of a way
I'm loading some HTML via Ajax with this format: <div id=div1> ... some content

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.