I want to convert the time when i echo from database to .php page.
When i upload a csv to database the table row with duration looks like this:
TABLE ROW:
|Duration|
500 > 5:00
234 > 2:34
1100 > 11:00
520 > 5:20
1300 > 13:00
10000 > 1:00:00
So what i want is to show it like this > 5:00.
If i change the TABLE ROW Duration in to TIME than i get this 00:05:00.
So what do i need to change it like above explained ?
Hopefully I explained it well
Thanks in advance.
<?php
foreach (array(500, 234, 1100, 520, 1300, 10000) as $number) {
echo $number, "\t";
$number = str_pad($number, 6, 0, STR_PAD_LEFT);
echo ltrim($number{0}.$number{1}.':'.$number{2}.$number{3}.':'.$number{4}.$number{5}, ":0"), "\n";
}
http://codepad.org/FrjWtgqx
Function wordwrap() could be useful:
foreach (['500', '234', '1100', '520', '1300', '10000'] as $number) {
$time = strrev($number);
$time = wordwrap($time, 2, ':', true);
$time = strrev($time);
echo "$number was transformed to $time\n";
}
demo
Related
I've been looking high and low for this, but I can't seem to find the best way to do this?
I want to basically run through a simple range of numbers 1-20, and every time there is a "3" listed, like 3 or 13, replace that with a value like "thisisa3value"
I'm just a little stumped on the best way that can be done?
This is the code I have so far but it doesn't seem to be working, as it basically prints this out on each number. I want it to basically just ONLY do that for ones that have a 3 associated with it. Can someone please take a look and let me know what I'm doing wrong?
<?php
foreach (range(1, 20) as $number ) {
echo $number;
echo ' ';
if ( in_array(3, range(1,20)) ) {
echo ' thisisa3value ';
}
}
?>
To reiterate, I just want it to basically print out like this:
1, 2, this is a 3 value, 4, 5, 6, 7, 8, 9, 10, 11, 12, this is a 3 value, 14, 15, 16, 17, 18, 29, 20.
Any help would be greatly appreciated. Thanks!
This Code may help you
foreach (range(1, 20) as $number ) {
echo $number;
echo '<br>';
if (strstr($number, '3')) {
echo ' thisisa3value <br>';
}
}
Based on your example, any appearance of a 3 is valid, such as 13.
$text = join(', ', range(1, 20));
echo preg_replace('/(\d*3\d*)/', 'this is a 3 value', $text);
Produces:
1, 2, this is a 3 value, 4, 5, 6, 7, 8, 9, 10, 11, 12, this is a 3 value, 14, 15, 16, 17, 18, 19, 20
Using loop to find the digit in that particular number
foreach (range(1, 20) as $number ) {
if( isDigitPresent($number, 3)){
echo 'thisisa3value ,';
}
else{
echo $number . ' ,' ;
}
}
function isDigitPresent( $number , $digit ){
$found = false;
while( $number > 0 ){
if( ($number % 10) == $digit ){
$found = true;
break;
}
$number = $number/10;
}
return $found ? $found : false;
}
How to always show less than 2 position int number using php ?
This below code
<?PHP
for($i=0;$i<=100;$i++)
{
echo $i."<BR>";
}
?>
result will be like this
0
1
2
3
4
5
6
7
8
9
10
.
.
.
100
I want to always show less than 2 position int number. like this how can i apply my php code ?
01
02
03
04
05
06
07
08
09
10
.
.
.
100
Just paste the lines inside your loop
if ($i < 10) {
$i= str_pad($i, 2, "0", STR_PAD_LEFT);
}
And print $i.
I don't know for sure if this works, but you can try it like this:
for($i=0;$i<=100;$i++)
{
if($i < 10) {
$i = "0$i";
echo $i;
}
else {
echo $i."<BR>";
}
}
You can use the function sprintf or the function str_pad like this ...
<?PHP
for ($i = 0; $i <= 100; $i++)
{
echo sprintf('%02d', $i) . "<BR>";
}
?>
... or this ...
<?PHP
for ($i = 0; $i <= 100; $i++)
{
echo str_pad($i, 2, '0', STR_PAD_LEFT) . "<BR>";
}
?>
Credits: https://stackoverflow.com/a/1699980/5755166
You could checkout the sprintf function that allows you to format the output http://php.net/manual/en/function.sprintf.php
Something like this perhaps
echo sprintf("%'.02d\n", 1);
You can use str_pad for adding 0's:
str_pad($var, 2, '0', STR_PAD_LEFT);
The 0 will not be added if the length is greater or equal 2.
In text, i have a lot of time number, then i want to change hour to another timezone (+6), example :
00:15 => 06:15
01:00 => 07:00
... and so on.
I'm trying this :
$result = str_replace(
array("00:","01:","02:","03:","04:","05:","06:","07:","08:","09:","10:","11:","12:","13:","14:","15:","16:","17:","18:","19:","20:","21:","22:","23:"),
array("06:","07:","08:","09:","10:","11:","12:","13:","14:","15:","16:","17:","18:","19:","20:","21:","22:","23:","00:","01:","02:","03:","04:", "05:"),
$text
);
echo $result;
But 18: will replace with 04: because php replace 18: to 22: then continue replace 22: to 04:
How to solved this, thank you.
// Edit : To #user3414969 and #Medda86: $text is the data i'm get from another site, that mean i can not control the source, only way to do is replace
// Edit 2 : Here is content : http://bongdatv.net/test.php
// Edit 3: Please solve this problem with replace way, not calculation number way.
I think best is to use the timestamp format, add the time and get out the new time from that.
http://php.net/manual/en/function.time.php
$time = array("00:","01:","02:","03:","04:","05:","06:","07:","08:","09:","10:","11:","12:","13:","14:","15:","16:","17:","18:","19:","20:","21:","22:","23:");
$required_time = array();
foreach($time as $t){
$hour = $t."00"; // 00 appending 0 minites
$hours_plus = 6; // adding 6 hours
$required_time[] = date('H:', strtotime($hour)+($hours_plus*60*60));
}
echo "<pre>";
print_r($required_time);
echo "</pre>";
Optimal way is as suggested by Medda86
However, you can try upon this way
$array = ("00:","01:",....);
//Then you can loop over array and add the time
for($i=0 ; $i < sizeof($array);$i++){
$array[$i] = intval($array[$i]+6)%24;
if($array[$i] < 10)
$array[$i] = str_pad($array[$i],2,'0',STR_PAD_LEFT).':';
else
$array[$i] .= ':';
}
Try this:
$yourArr = array('00:15','01:00','00:30');
foreach ($yourArr as $key => $value) {
$timestamp = strtotime($value) + 60*60*6; // add hours as per your need.
$time = date('H:i', $timestamp);
$newArr[] = $time;
}
echo "<pre>";
print_r($newArr);
Result is:
Array
(
[0] => 06:15
[1] => 07:00
[2] => 06:30
)
I have two arrays of matching data. One with the values and the other with the time. I round time to the nearest 5 minutes and in result I have a new array with hours that repeat themself es. I group the equal hours, count them, summerize their respective values in the other array and take an average for that hour. here is the code:
$first_hour = array('11:01', '12:04', '13:00', '15:28', "15:43", "15:53", "15:55", "16:02", "16:05", "16:17", "16:15", "16:21", "16:25", "16:33", "16:35", "16:43", "16:45", "16:56", "16:58", "17:00", "17:04", "17:07", "17:19");
$values = array(12, 23, 5, 90, 12, 23, 45, 56, 12, 15, 43, 48, 54, 62, 52, 41, 74, 54, 84, 75, 96, 69, 36);
$minutes = 5;
$precision = 60 * $minutes;
$time = "00:00";
$average = 0;
//round first array to nearest 5 minutes
$timestamp = strtotime($first_hour);
$data_ora = date("H,i", round($timestamp / $precision) * $precision);
//the hours array will become like this:
$first_hour = array('11:00', '12:05', '13:00', '15:30', "15:45", "15:50", "15:55", "16:00", "16:05", "16:15", "16:15", "16:20", "16:25", "16:35", "16:35", "16:45", "16:45", "17:00", "17:00", "17:00", "17:05", "17:10", "17:20");
if ($first_hour == $time) {
if ($values[$i] > 0) {
$indexi = $indexi + 1;
$total = $total + $values;
echo "</br> total = ".$total;
$i++;
} else {
$time = date("H,i", round($timestamp / $precision) * $precision);
//echo "time has changed";
$average = $total / $index;
$dataora = date("Y,m,d", round($timestamp)) . "," . $time;
//fill in the vectors
$v[1] = $average;
$v[2] = $dataora;
echo "</br> average = " . $v[1];
$indexi = 0;
$totali = 0;
}
}
The output is like this:
"total = 2996
total = 3325
total = 3656
total = 3996
average = 333
total = 329
total = 652
total = 976
total = 1304
total = 1630
total = 1961
total = 2297
total = 2629
average = 328.625
total = 332
total = 660
total = 997
total = 1320
total = 1646
total = 1967
For each group there is an average calculated, except for the last group of values. How can I calculate its average? For the last group it gets into the first condition but it doesn't take a break (change hour) to fall into the else so that the average is calculated.
I'm almost certain the sample code you have provided doesn't match your real code, so I'm not going to try and fix that, but I will try and explain how you might fix the problem you claim to have in your real code.
Assumedly you have a loop of some sort that is iterating over the values in the $values array. I also assume that some of the values in that array are less than or equal to zero, and those are the points at which you display the average.
Your problem then is that your loop reaches the end of the array without having a chance to display the average for the last set of values.
One easy solution, is to add a zero to the end of the array if there isn't already one there. That way the last value the loop encounters will always be a zero, which will match the condition needed to display an average.
If that is not feasible, you could make your loop not stop when it reaches the end of the array, so if it's a while loop, it essentially becomes:
while (true) ...
Then you change your first condition to this:
if ($i < count($values) && $values[$i] > 0) {
so it only matches if you haven't gone past the end of the array. Otherwise it falls through to displaying the average.
And at the end of the average code, you add another check to to see if you're past the end of the array and break out of the loop.
if ($i >= count($values)) break;
These changes alone won't fix the sample code you've provided, but it might help if your real code matches the functionality you've described in the question.
As the title says what I'm trying to do is count the number of images in a directory and output it as numbers so for example if there are 4 images I want the result to be:
01 | 02 | 03 | 04
I have this so far:
$count = glob('images/{*.jpg}', GLOB_BRACE);
foreach($count as $filecount) {
echo '<li>' . $filecount . '</li>';
}
which outputs path/filename.jpg but haven't a clue on how to convert that to a numbers array or even if i'm in the right ballpark.
As usual all help is appreciated and thanks in advance.
That array is numerically indexed (0 to length-1 ), use it to obtain the number:
foreach($count as $index => $filecount) {
$number = $index+1;
foreach($count as $index => $filecount) {
// $number is "01" for the first and "02" for second etc
$number = str_pad($index, 2, "0", STR_PAD_LEFT);
//...
}