TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   convert time format (http://www.talkphp.com/absolute-beginners/1692-convert-time-format.html)

meshi 12-10-2007 09:46 AM

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?

Geert 12-10-2007 10:04 AM

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.

Haris 12-10-2007 10:13 AM

Quote:

Originally Posted by meshi (Post 6143)
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 (Post 6143)
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);
}

Wildhoney 12-10-2007 10:44 AM

Haris is on fire today I see :-) ! Very nice Haris, kudos for that!

meshi 12-10-2007 11:01 AM

thanks guyz..

Wildhoney 12-10-2007 10:24 PM

Is there any reason you take the longer approach, Geert?

Salathe 12-10-2007 10:59 PM

Quote:

Originally Posted by Haris (Post 6148)
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! :-P

php Code:
foreach (range(1, 200) as $iInteger)
{
    printf('%02d', $iInteger);
}

Jay 12-11-2007 12:47 AM

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 ^^

Village Idiot 12-11-2007 02:42 AM

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

Haris 12-11-2007 02:46 AM

Quote:

Originally Posted by Salathe (Post 6203)
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! :-P

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. :-P

Geert 12-11-2007 07:28 PM

Quote:

Originally Posted by Wildhoney (Post 6198)
Is there any reason you take the longer approach, Geert?

I was bitten by the overlong function snake. :-P

Wildhoney 12-11-2007 07:29 PM

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 (Post 6205)
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.

ReSpawN 12-11-2007 07:55 PM

Quote:

Originally Posted by Wildhoney (Post 6306)
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%"' 


Salathe 12-11-2007 09:10 PM

Quote:

Originally Posted by Haris (Post 6216)
The PHP document inspired me to use the echo.

http://www.php.net/sprintf, start nagging them. :-P

That's allowed since it is the sprintf documentation page. :-)


All times are GMT. The time now is 05:22 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0