?
Solved

PHP preg_replace for Time

Posted on 2011-03-03
5
Medium Priority
?
402 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 500 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

Independent Software Vendors: 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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 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