Solved

PHP preg_replace for Time

Posted on 2011-03-03
5
401 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
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.
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 …

688 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