I search for my problem on php.net and on stackoverflow and I did'nt find a good answer to solve my problem, so I decide to ask it !
I have a number of secs :
210
I transform it to 00:03:30 :
gmdate('H:i:s', 210);
But how can I format my answer like :
3m30s or 3min30secs ?
I think I can make this transformation by Exploding my results and concatenate with variable, but I don't know if it's the best solution...
$part = explode(":", $mytime);
$hours = $part[0];
$mins = $part[1];
$secs = $part[2];
$hoursvar = "h";
$minsvar = "m";
$secsvar = "s";
$timefinal = $hours.$hoursvar.$mins.$minsvar.$secs.$secsvar;
Can anyone help me? Thanks.
echo ltrim(date("H", 210), "0")." hours, ".
ltrim(date('i', 210), "0")." min, ".
ltrim(date('s', 210), "0")." secs";
You can format the string inside gmdate, just escape the characters you want to print:
$sec = 210;
echo gmdate("i\m s\s", $sec);
Output:
03m 30s
date() works a little bit like printf(). You pass a format string containing a defined set of meta characters which will be replaced by values. Note that the format string can contain any content not just meta characters. The manual page explains this as well. You can use:
date('i\ms', 210);
You see that you have to escape literals (the m) to that they were not replaced by values.
I hope you can live with a leading zero : 03m30
if not, use:
$date = date('i\ms', 210);
if(strpos($date, '0') === 0) {
$date = substr($date, -strlen($date) + 1);
}
Or, shorter, thanks #Orangepill :) :
$date = ltrim(date('i\ms', 210), '0');
as there is no meta character for minutes without leading zeros known by date().
This is easy, your current date format is H:i:s and you want to convert it to i:s
So you do the following
$date = "00:03:30";
$new_date = date("i_s-",strtotime($date));
Since we can't insert the letters m and s in the function we can use str_replace to replace the _ with m and replace - with s so
$new_date = str_replace("_","m",$new_date);
$new_date = str_replace("-","s",$new_date);
That was the best way I could think of.
$string_time = '00:03:30';
$int_time = strtotime(date('Y-m-d').' '.$string_time);
echo date('i', $int_time).'m'.date('s', $int_time).'s';
$seconds = 210;
$date = gmdate("H\h:i\m:s\s", $seconds);
echo preg_replace('/0([0-9])/','$1', $date);
OUTPUT
0h:3m:30s
You can use gmdate with preg_replace to remove 0 from begining.
OR more advanced
$seconds = 210;
$date = gmdate("H\h:i\m:s\s", $seconds);
$match = array('/0([0-9])/','/s/','/m\:/','/h\:/');
$replace = array('$1', ' sec ', ' min ', ' hours ');
echo preg_replace($match,$replace, $date);
OUTPUT:
0 hours 3 min 30 sec
echo ltrim(date("H", 210), "0")." hours, ".
ltrim(date('i', 210), "0")." min, ".
ltrim(date('s', 210), "0")." secs";
Thanks to Orangepill !
Related
Is there any way to convert "01:10:00" string into the following format "1 h:10 min" using php? I did this way, but I need to do the opposite.
'date('H:i:s',strtotime('1 hour 1 minute 1 second', strtotime('midnight')))'
There're endless ways, e.g.:
$input = '01:10:00';
list($h, $m, $s) = explode(':', $input);
$output = "$h h:$m min";
You can also tweak output format in different ways, e.g.
$output = sprintf("%d h:%02d min", $h, $m);
... will display $h as integer and $m as two-character integer with leading zeroes. It's all just text at this point (don't get confused by the concept of "time").
Try this:
$time = '01:10:00';
$date = DateTime::createFromFormat('H:i:s', $time);
echo $date->format('H \h:i \m\i\n');
My client has entered some start and end dates into our system and I want to be able to format it with PHP.
The values are 930 and 1530. Processing the 1530 variable is fine but its the 930 that returns false.
Here is my script so far but no success. Error returns bool(false) because it can't get a readable time (I believe?)
$time = DateTime::createFromFormat('Hi', $var);
$format = "H:i";
$result = $time->format($format);
That's because your initial time format is ambiguous. Assuming that you have time without leading zeros all the time one can do something like this:
$var = '930';
$time = DateTime::createFromFormat('Hi', str_pad($var, 4, '0', STR_PAD_LEFT));
$format = "H:i";
$result = $time->format($format);
I wouldn't bother with DateTime for this, split it and join it like this:
$var = 1530;
$minutes = substr($var, -2, 2);
$hours = str_replace($minutes, '', $var);
$time = $hours . ':' . $minutes;
Then use DateTime on the string:
$dateTime = new DateTime($time);
$time = $dateTime->format('H:i');
I have a PHP Script that has an INT in the format MMSSMS - Minutes Seconds Milliseconds defined as $time. I just need a : every 2 places in $time. Any help is appreciated.
Example: $message = 'I clocked in (MM:SS:MS) '.$time.';
the issue is the $time displays as 000044 instead 00:00:44 on echo
This code replaces 2 consecutive digits not on the end of the string with the digits followed by colon
$message = 'I clocked in (MM:SS:MS) '.preg_replace('/\d{2}(?!$)/','$0:',$time);
php.net/preg_replace
demo: http://sandbox.onlinephpfunctions.com/code/de0ce1e4466ff747f1bba2ac92331623a1b234ed
<?php
$time = '123456';
$str = strval($v);
$time = $str[0].$str[1].":".$str[2].$str[3].":".$str[4].$str[5];
echo $time;
?>
Hope this helps:
$time = '001122';
$minutes = substr("$time", 0, 2);
$seconds = substr("$time", 2, 2);
$mills = substr("$time", 4, 2);
echo $newTime = "$minutes:$seconds:$mills";
You can use chunk_split() in conjunction with rtrim()
$time = '000044';
$time = rtrim(chunk_split($time, 2, ':'), ':');
Edit
If $time is an INT, you would need to convert using (string)$time or strval($time). However, in your example you are using 000044, so I am assuming $time is already a string...
In php is there a way to turn two integers (09 and 30) into into type time (9:30). I need this function so I can INSERT these number into a column in MySQL of type time.
setTime
$datetime = new DateTime();
$datetime->setTime(9, 30, 0);
DateTime::format ( string $format )
will do
$str = '09:' +'30';
$date = DateTime($str);
$date->format('H:i')
In php is there a way to turn two integers (09 and 30) into into type time (9:30). I need this function so I can INSERT these number into a column in MySQL of type time.
Yes, by formating them as string:
$hr = 9;
$min = 30;
$time = sprintf('%02d:%02d', $hr, $min);
echo $time; # 09:30
As hakre said, use sprintf.
$hours = 9;
$minutes = 30;
$time = sprintf("%02d:%02d:00", $hours, $minutes);
$a1 = array("09","30");
$date implode(":",$a1);
I have a piece of PHP that is trying to do this:
1) given a string like "h m s" (where h=hr, m=min, s=sec)
2) Add the time from 1) to time()
3) format the result to look like "y-mth-d-h-min-s"
So say the time is now 01-01-2011 1am, I want it to add "10 0 0", which should give me 01-01-2011 11am, but for some reason at the moment, it does seem to add the string, but it's not accurate.
This is the code I'm using:
$values_arr['regx_expdate'] = date("Y-m-d H:i:s", time()+$values_arr['regx_expdate']);
where $values_arr['regx_expdate'] is the string in the format "h m s", eg. "10 0 0".
The main question is how would time() know if "10 0 0" is actually 10hrs 0min 0min, and not 10days 0hr 0min??
It does not.
It will cast it to int, interpret it as seconds and add it to the result of time().
Some code that could do as you describe would be:
list ($h,$m,$s) = explode(' ', $values_arr['regx_expdate'], 3);
$difference = 60*60*$h + 60*$m + $s;
$values_arr['regx_expdate'] = date("Y-m-d H:i:s", time()+$difference);
Easiest method I can think of is to extract each token from $values_arr['regx_expdate'], add up the seconds and simple add it to time().
For example
if (preg_match('/^(\d{1,2}) (\d{1,2}) (\d{1,2})$/', $values_arr['regx_expdate'], $units)) {
$seconds = $units[3] + ($units[2] * 60) + ($units[1] * 3600);
$newTimestamp = time() + $seconds;
}
After parsing your input string into Hours Minutes and Seconds, it might be worth reorganizing said array of values into a string that PHP's strtotime can process.
This little function may help, you may customize it to suit your purpose:
function AddingDaysFromNow($number_of_days)
{
$today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
// today is now time return in seconds
$addingTime = $today + (86400 * $number_of_days);
// adding to advance it
//choice a date form at here
return date("Y-m-d", $addingTime);
}
//use it as
$expireDate = AddingDaysFromNow(2); // assume the 2 is advance to 2 days ahead
// return the day in future
Good luck!
To handle and convert dates in php you should first force everything into unixtimestamp and then you give it the structure you want
$date = date("THE-DATE-FORMAT-YOU-WANT", "THE-DATE-YOU-WOULD-LIKE-TO-CONVERT-IN-SECONDS");
//For example.
$new_date = date("Y-m-d H:i:s", strtotime($old_date));
$now = date("Y-m-d H:i:s", time());