 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-10-2007, 09:46 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Posts: 44
Thanks: 0
|
convert time format
hi guyz i need ur help!!
how could i convert the time format for example i have a time 4:56 pm and i want to convert it in 16:56 so that the if i query it from the database it displays the result.?
on more thing how would i iterate in this format
00
01
02
03
04
05
06
07
08
09
if i use this:
for (i=0;i>10;i++)
the result would be
123456789
how would i put 0's in my iteration so that it would be 2 numbers?
|
|
|
|
12-10-2007, 10:04 AM
|
#2 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
|
Copied from Kohana's date helper:
PHP Code:
/**
* Adjusts a non-24-hour number into a 24-hour number.
*
* @param integer hour to adjust
* @param string AM or PM
* @return string
*/
public static function adjust($hour, $ampm)
{
$hour = (int) $hour;
$ampm = strtolower($ampm);
switch($ampm)
{
case 'am':
if ($hour == 12)
$hour = 0;
break;
case 'pm':
if ($hour < 12)
$hour += 12;
break;
}
return sprintf('%02s', $hour);
}
As you can see, you can use sprintf to autofill a number with zeroes.
|
|
|
|
12-10-2007, 10:13 AM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Quote:
Originally Posted by meshi
hi guyz i need ur help!!
how could i convert the time format for example i have a time 4:56 pm and i want to convert it in 16:56 so that the if i query it from the database it displays the result.?
|
Quote:
Originally Posted by meshi
on more thing how would i iterate in this format
00
01
02
03
04
05
06
07
08
09
if i use this:
for (i=0;i>10;i++)
the result would be
123456789
how would i put 0's in my iteration so that it would be 2 numbers?
|
php Code:
foreach(range(1, 200) as $iInteger){ sprintf("%02d", $iInteger); }
__________________
Necessity is the mother of invention.
My blog
Last edited by Haris : 12-11-2007 at 02:42 AM.
|
|
|
|
|
The Following 2 Users Say Thank You to Haris For This Useful Post:
|
|
12-10-2007, 10:44 AM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Haris is on fire today I see  ! Very nice Haris, kudos for that!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-10-2007, 11:01 AM
|
#5 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Posts: 44
Thanks: 0
|
thanks guyz..
|
|
|
|
12-10-2007, 10:24 PM
|
#6 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Is there any reason you take the longer approach, Geert?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-10-2007, 10:59 PM
|
#7 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Haris
|
Regarding echo sprintf, what's that all about? Also, there is no need in this case for double quotes around the formatting string. I'm sure this isn't the first time I've seen echo sprintf posted up here, so every time I see it, I'll keep nagging!
php Code:
foreach (range(1, 200) as $iInteger){ printf('%02d', $iInteger); }
Last edited by Salathe : 12-11-2007 at 01:59 AM.
|
|
|
|
|
The Following 2 Users Say Thank You to Salathe For This Useful Post:
|
|
12-11-2007, 12:47 AM
|
#8 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
I've never found a reason to use double quotes yet.. have you?
Some people like to do
PHP Code:
$foo = "Welcome, {$bar}.";
However if you have ever bench-marked the concatenation speed of that, and compare it to the speed of
PHP Code:
$foo = 'Welcome, ' . $bar;
You will find that the single-quote concatenation is faster 
|
|
|
|
12-11-2007, 02:42 AM
|
#9 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Why don't you just use a unix timestamp? You can output that in any formate with the date() function.
|
|
|
|
12-11-2007, 02:46 AM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Quote:
Originally Posted by Salathe
Regarding echo sprintf, what's that all about? Also, there is no need in this case for double quotes around the formatting string. I'm sure this isn't the first time I've seen echo sprintf posted up here, so every time I see it, I'll keep nagging!
php Code:
foreach (range(1, 200) as $iInteger){ printf('%02d', $iInteger); }
|
The PHP document inspired me to use the echo.
Quote:
Example#9 sprintf(): scientific notation
<?php
$number = 362525200;
echo sprintf("%.3e", $number); // outputs 3.625e+8
?>
|
http://www.php.net/sprintf, start nagging them. 
__________________
Necessity is the mother of invention.
My blog
|
|
|
|
12-11-2007, 07:28 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
|
Quote:
Originally Posted by Wildhoney
Is there any reason you take the longer approach, Geert?
|
I was bitten by the overlong function snake. 
|
|
|
|
12-11-2007, 07:29 PM
|
#12 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Lol! I can see those monsters playing a big part in this community  I'm sure we all have a few of those monsters in our code.
Quote:
Originally Posted by Jay
I've never found a reason to use double quotes yet.. have you?
Some people like to do
PHP Code:
$foo = "Welcome, {$bar}.";
However if you have ever bench-marked the concatenation speed of that, and compare it to the speed of
PHP Code:
$foo = 'Welcome, ' . $bar;
You will find that the single-quote concatenation is faster 
|
I've never had a need for double quotes except when using \r and \n.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
12-11-2007, 07:55 PM
|
#13 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
Quote:
Originally Posted by Wildhoney
Lol! I can see those monsters playing a big part in this community  I'm sure we all have a few of those monsters in our code.
I've never had a need for double quotes except when using \r and \n.
|
Good point. I've only been using it in javascript (for popups and ofcourse alerts/confirms). Next to that, can't recall where I used it. Oh, I used it for
PHP Code:
$rSql = 'SELECT * FROM prefix_table WHERE username LIKE "%$user%"'
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
12-11-2007, 09:10 PM
|
#14 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Haris
|
That's allowed since it is the sprintf documentation page. 
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|