Solved

PHP preg_replace for Time

Posted on 2011-03-03
5
400 Views
Last Modified: 2012-06-27
I need a PHP function[s] (? preg_replace) that will convert different entered time formats to the standard hh:mm.
Possible entered formats include:
hh:mm
h:mm
hh.mm
h.mm
hhmm
hmm
h
0
Comment
Question by:gorrie67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35033710
Something like this should work a treat for all your examples. Will test it shortly.
$patterns = array("/^(\d)\D?(\d\d)$/", #one digit hour
                  "/^(\d\d)\D?(\d\d)$/", #two digit hour
                  "/^(\d)$/");
$replacements = array("0$1:$2",
                      "$1:$2",
                      "$1:00");
$result = preg_replace($patterns, $replacements, $time);

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35033733
It passed testing as is with your example data. To wrap it in a function:
function to_std_time($time) {
  $patterns = array("/^(\d)\D?(\d\d)$/", #one digit hour
                    "/^(\d\d)\D?(\d\d)$/", #two digit hour
                    "/^(\d)$/");
  $replacements = array("0$1:$2",
                        "$1:$2",
                        "$1:00");
  return preg_replace($patterns, $replacements, $time);
}

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35033736
Of course you might like to add criteria to handle invalid formats too.
0
 

Author Comment

by:gorrie67
ID: 35033751
Thanks TerryAtOpus

All works fine, except for inputs of hh only, ie 8, 12, 13

these need to be formatted to hh:mm, ie 08:00, 12:00, 13:00

Can this be added to function?
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 125 total points
ID: 35033757

function to_std_time($time) {
  $patterns = array("/^(\d)\D?(\d\d)$/", #one digit hour
                    "/^(\d\d)\D?(\d\d)$/", #two digit hour
                    "/^(\d)$/", #one digit hour, no minutes
                    "/^(\d\d)$/"); #two digit hour, no minutes
  $replacements = array("0$1:$2",
                        "$1:$2",
                        "0$1:00",
                        "$1:00");
  return preg_replace($patterns, $replacements, $time);
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Compiling PHP with Curl plus protocols 8 48
Get data from two MySQL tables 6 65
Form submit takes only for one form 23 51
integrated two Separate file . 3 30
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question