Re: [PHP] Displaying 2 digit minutes/seconds

2009-08-20 Thread Jonathan Tapicer
You can use sprintf or str_pad to fill in with zeros, with sprintf you
can do this:

echo sprintf('%02d', 5);

That will print the string 05.

Jonathan

On Thu, Aug 20, 2009 at 6:27 PM, sono...@fannullone.us wrote:
 Hi all,

        I'm using this code to display the current time for our location on
 our website:

 ?php
        date_default_timezone_set('America/Los_Angeles');
        $theTimeIs = getdate(time());
            $theHour = $theTimeIs['hours'];
            $theMinute = $theTimeIs['minutes'];  // make minutes under 10
 show two digits
            $theSecond = $theTimeIs['seconds'];
        if($theHour  12){
            $theHour = $theHour - 12;
            $dn = PM;
        } else {
            $dn = AM;
        }

 echo $theHour:$theMinute:$theSecond $dn;
 ?

        It works great except for one small detail.  If the time is 3:04:02,
 it is displayed as 3:4:2 which, of course, is very odd looking.  So I
 corrected it as follows:

 ?php
        date_default_timezone_set('America/Los_Angeles');
        $theTimeIs = getdate(time());
            $theHour = $theTimeIs['hours'];
            if (strlen ($theTimeIs['minutes'])  2) {
                                $theMinute = 0 . $theTimeIs['minutes'];
                                } else {
                                $theMinute = $theTimeIs['minutes'];
                                }
            if (strlen ($theTimeIs['seconds'])  2) {
                                $theSecond = 0 . $theTimeIs['seconds'];
                                } else {
                                $theSecond = $theTimeIs['seconds'];
                                }
        if($theHour  12){
            $theHour = $theHour - 12;
            $dn = PM;
        } else {
            $dn = AM;
        }

 echo $theHour:$theMinute:$theSecond $dn;
 ?

        It works, but is there a better way to do it?

 Thanks,
 Frank

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Displaying 2 digit minutes/seconds

2009-08-20 Thread Ashley Sheridan
On Thu, 2009-08-20 at 14:27 -0700, sono...@fannullone.us wrote:
 Hi all,
 
   I'm using this code to display the current time for our location on  
 our website:
 
 ?php
   date_default_timezone_set('America/Los_Angeles');
  $theTimeIs = getdate(time());
  $theHour = $theTimeIs['hours'];
  $theMinute = $theTimeIs['minutes'];  // make minutes  
 under 10 show two digits
  $theSecond = $theTimeIs['seconds'];
  if($theHour  12){
  $theHour = $theHour - 12;
  $dn = PM;
  } else {
  $dn = AM;
  }
 
 echo $theHour:$theMinute:$theSecond $dn;
 ?
 
   It works great except for one small detail.  If the time is 3:04:02,  
 it is displayed as 3:4:2 which, of course, is very odd looking.  So I  
 corrected it as follows:
 
 ?php
   date_default_timezone_set('America/Los_Angeles');
  $theTimeIs = getdate(time());
  $theHour = $theTimeIs['hours'];
  if (strlen ($theTimeIs['minutes'])  2) {
   $theMinute = 0 . $theTimeIs['minutes'];
   } else {
   $theMinute = $theTimeIs['minutes'];
   }
  if (strlen ($theTimeIs['seconds'])  2) {
   $theSecond = 0 . $theTimeIs['seconds'];
   } else {
   $theSecond = $theTimeIs['seconds'];
   }
  if($theHour  12){
  $theHour = $theHour - 12;
  $dn = PM;
  } else {
  $dn = AM;
  }
 
 echo $theHour:$theMinute:$theSecond $dn;
 ?
 
   It works, but is there a better way to do it?
 
 Thanks,
 Frank
 

What's wrong with using the date() function? You can have it output any
sort of format you wish. So, getting a 2 digit time in
hours:minutes:seconds you would put:

date(H:i:s);

Thanks,
Ash
http://www.ashleysheridan.co.uk




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Displaying 2 digit minutes/seconds

2009-08-20 Thread sono-io


On Aug 20, 2009, at 2:38 PM, Ashley Sheridan wrote:

What's wrong with using the date() function? You can have it output  
any

sort of format you wish. So, getting a 2 digit time in
hours:minutes:seconds you would put:

date(H:i:s);


	Thanks, Ash.  I had tried that before but I couldn't find a way to  
make it display in 12 hour time, so I went with the other method.  I  
guess I didn't look hard enough. =;)


This works:

echo (date(g:i A));

Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Displaying 2 digit minutes/seconds

2009-08-20 Thread sono-io


On Aug 20, 2009, at 2:34 PM, Jonathan Tapicer wrote:

You can use sprintf or str_pad to fill in with zeros, with sprintf  
you can do this:


echo sprintf('%02d', 5);


	Thanks, Jonathan!  I learned two new functions today!  Both work  
great but I think I like sprintf for this application better since  
it's more succinct.


echo sprintf('%02d', $theMinute);

echo (str_pad($theMinute,2,0,STR_PAD_LEFT));

Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Displaying 2 digit minutes/seconds

2009-08-20 Thread Daevid Vincent
 

 -Original Message-
 From: sono...@fannullone.us [mailto:sono...@fannullone.us] 
 Sent: Thursday, August 20, 2009 3:53 PM
 To: Jonathan Tapicer; PHP General List
 Subject: Re: [PHP] Displaying 2 digit minutes/seconds
 
 
 On Aug 20, 2009, at 2:34 PM, Jonathan Tapicer wrote:
 
  You can use sprintf or str_pad to fill in with zeros, with sprintf  
  you can do this:
 
  echo sprintf('%02d', 5);
 
   Thanks, Jonathan!  I learned two new functions today!  
 Both work  
 great but I think I like sprintf for this application better since  
 it's more succinct.
 
 echo sprintf('%02d', $theMinute);

Uh. If you MUST do this nonsense, then at least do this instead and not
echo sprintf:

printf('%02d', $theMinute);


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php