Solved

remove hours if hours=0

Posted on 2013-06-26
7
191 Views
Last Modified: 2013-07-01
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
Comment
Question by:rgb192
7 Comments
 
LVL 9

Accepted Solution

by:
TvMpt earned 500 total points
Comment Utility
echo preg_replace('/(^(0:))/m',' ',$sourcestring);

Open in new window

0
 
LVL 11

Expert Comment

by:mcnute
Comment Utility
you could do,

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

Open in new window

0
 
LVL 4

Expert Comment

by:ramyajanarthanan
Comment Utility
I hope you import files to your db from the excel sheets.Please may i know your requirement clearly.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:Robert Saylor
Comment Utility
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 108

Expert Comment

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

by:rgb192
Comment Utility
i did not understand the time() command (where to put variable)

so this preg_replace worked

thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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);
}

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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.

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

11 Experts available now in Live!

Get 1:1 Help Now