Solved

PHP preg_replace for Time

Posted on 2011-03-03
5
393 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
  • 4
5 Comments
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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
Comment Utility
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
Comment Utility
Of course you might like to add criteria to handle invalid formats too.
0
 

Author Comment

by:gorrie67
Comment Utility
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
Comment Utility

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now