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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:40:02+00:00 2026-06-01T08:40:02+00:00

how can i replace date/time in this format ‘Fri Mar 23 15:21:08 2012’ with

  • 0

how can i replace date/time in this format ‘Fri Mar 23 15:21:08 2012’ with preg_replace?
Date in this format is present couple of times in my text and i need to replace it with current time/date.

Thanks,

Chris

  • 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-06-01T08:40:03+00:00Added an answer on June 1, 2026 at 8:40 am

    Well, what you need is an expression that will match 3 letters (Fri) followed by a space and another three letters (Mar).

    First we need to match some letters:

    /[a-z]/
    

    We can match exactly 3 letters like this:

    /[a-z]{3}/
    

    …and we’ll need it to be case insensitive:

    /[a-z]{3}/i
    

    …so the first part is just:

    /[a-z]{3} [a-z]{3}/i
    

    Next, we need to match either 1 or 2 numerics. A numeric can be represented with the escape sequence \d, so we’d use:

    /\d{1,2}/
    

    Next we match the time string, using the same escape sequence:

    /\d{2}:\d{2}:\d{2}/
    

    …followed by a final 4 digit year:

    /\d{4}/
    

    Put it all together and we get:

    /[a-z]{3} [a-z]{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4}/i
    // Fri       Mar      23     15 :  21 :  08    2012
    

    Now, we need to replace it with the current date and time. The usual place we’d go for that is the date() function, but how to we get that into the replacement dynamically? Well we could pass it as a string literal, or we could use a callback function to get it from preg_replace_callback(). But, preg_replace() gives us the e modifier which causes the replacement string to be evaluated for PHP code. We have to be careful and sparing with it’s use, as with any PHP eval(), but this is a legitimate use case.

    So our final PHP code looks like this:

    preg_replace(
      '/[a-z]{3} [a-z]{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4}/ie',
      "date('D M j H:i:s Y')",
      $str
    );
    

    See it working

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

Sidebar

Related Questions

If I have a JSON DateTime: DateCreated:\/Date(1301692095627)\/ How can I format that into a
How can replace values as array by replace in javascript. I want done replace
How can replace arabic number with latin number as that doesn't change number into
so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
How i can replace some characters when user type a sentence in EditText? For
I need to know how I can replace the last s from a string
Is there a built-in .NET class that can replace or work like WinHttp.WinHttpRequest? Thanks!
I'd like a regexp or other string which can replace everything except alphanumeric chars
I was wondering if there is any way that I can replace substrings within
Is it possible to develop a plug-in for Internet Explorer that can replace the

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.