TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-10-2007, 09:46 AM   #1 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default 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?
meshi is offline  
Reply With Quote
Old 12-10-2007, 10:04 AM   #2 (permalink)
The Contributor
RegEx Guru 
 
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
Geert is on a distinguished road
Default

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.
__________________
Kohana - PHP5 framework
Geert is offline  
Reply With Quote
Old 12-10-2007, 10:13 AM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by meshi View Post
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.?
php Code:
$szTime = date("G:i:s", strtotime("4:56 PM"));

Quote:
Originally Posted by meshi View Post
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.
Haris is offline  
Reply With Quote
The Following 2 Users Say Thank You to Haris For This Useful Post:
Geert (12-10-2007), Wildhoney (12-10-2007)
Old 12-10-2007, 10:44 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-10-2007, 11:01 AM   #5 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default

thanks guyz..
meshi is offline  
Reply With Quote
Old 12-10-2007, 10:24 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-10-2007, 10:59 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Haris View Post
php Code:
foreach(range(1, 200) as $iInteger)
{
    echo sprintf("%02d", $iInteger);
}
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.
Salathe is offline  
Reply With Quote
The Following 2 Users Say Thank You to Salathe For This Useful Post:
Haris (12-11-2007), SOCK (12-11-2007)
Old 12-11-2007, 12:47 AM   #8 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

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
Jay is offline  
Reply With Quote
Old 12-11-2007, 02:42 AM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Why don't you just use a unix timestamp? You can output that in any formate with the date() function.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-11-2007, 02:46 AM   #10 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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
Haris is offline  
Reply With Quote
Old 12-11-2007, 07:28 PM   #11 (permalink)
The Contributor
RegEx Guru 
 
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
Geert is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Is there any reason you take the longer approach, Geert?
I was bitten by the overlong function snake.
__________________
Kohana - PHP5 framework
Geert is offline  
Reply With Quote
Old 12-11-2007, 07:29 PM   #12 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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 View Post
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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-11-2007, 07:55 PM   #13 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-11-2007, 09:10 PM   #14 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Haris View Post
The PHP document inspired me to use the echo.

http://www.php.net/sprintf, start nagging them.
That's allowed since it is the sprintf documentation page.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:04 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design