Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# remove hours if hours=0

Posted on 2013-06-26
Medium Priority
205 Views
input from excel file
0:00:02
0:50:04
0:04:08
1:20:30
0:00:09

remove
hours and :
if hours = 0

want output:
00:02
50:04
04:08
1:20:30
00:09
0
Question by:rgb192

LVL 9

Accepted Solution

TvMpt earned 2000 total points
ID: 39277348
``````echo preg_replace('/(^(0:))/m',' ',\$sourcestring);
``````
0

LVL 11

Expert Comment

ID: 39277350
you could do,

``````if date('H', time()) == 0
echo date('H:i', time());
``````
0

LVL 4

Expert Comment

ID: 39277418
I hope you import files to your db from the excel sheets.Please may i know your requirement clearly.
0

LVL 7

Expert Comment

ID: 39277715
It's best to convert the time to seconds then use a math equation to see if it is over 59 mins in seconds. Then you could use an else to display the H or not.
0

LVL 111

Expert Comment

ID: 39278296
The standard methods of handling date and time values are given in this article.  Se the parts about Time Without Date and Practical Application #3.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

With the test data posted here, I think TvMpt has an accurate solution.  But I think my instincts would lead me to keep the hours, even if they are zero.
0

Author Closing Comment

ID: 39290861
i did not understand the time() command (where to put variable)

so this preg_replace worked

thanks
0

LVL 111

Expert Comment

ID: 39290903
You do not need to understand the time() command.  The time() command is not the right tool to use.  This article explains the way to do date and time format conversions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

The quality of test data is usually more important than the programming.  You might want to run this to see what output you will get.

``````<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA WITH AN OUTLIER
\$arr = array
( '0:00:02'
, '0:50:04'
, '0:04:08'
, '1:20:30'
, '0:00:09'
, '00:00:09'
)
;

// TRY THE REGEX
foreach (\$arr as \$sourcestring)
{
echo PHP_EOL;
echo \$sourcestring;
echo ' => ';
echo preg_replace('/(^(0:))/m',' ',\$sourcestring);
}
``````
0

## Featured Post

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.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
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 …
###### Suggested Courses
Course of the Month11 days, 14 hours left to enroll