Displaying 2 digit minutes/seconds

Displaying 2 digit minutes/seconds

am 20.08.2009 23:27:06 von sono-io

Hi all,

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

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:

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

Re: Displaying 2 digit minutes/seconds

am 20.08.2009 23:34:52 von 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, wrote:
> Hi all,
>
> =A0 =A0 =A0 =A0I'm using this code to display the current time for our lo=
cation on
> our website:
>
> > =A0 =A0 =A0 =A0date_default_timezone_set('America/Los_Angeles');
> =A0 =A0 =A0 =A0$theTimeIs =3D getdate(time());
> =A0 =A0 =A0 =A0 =A0 =A0$theHour =3D $theTimeIs['hours'];
> =A0 =A0 =A0 =A0 =A0 =A0$theMinute =3D $theTimeIs['minutes']; =A0// make m=
inutes under 10
> show two digits
> =A0 =A0 =A0 =A0 =A0 =A0$theSecond =3D $theTimeIs['seconds'];
> =A0 =A0 =A0 =A0if($theHour > 12){
> =A0 =A0 =A0 =A0 =A0 =A0$theHour =3D $theHour - 12;
> =A0 =A0 =A0 =A0 =A0 =A0$dn =3D "PM";
> =A0 =A0 =A0 =A0} else {
> =A0 =A0 =A0 =A0 =A0 =A0$dn =3D "AM";
> =A0 =A0 =A0 =A0}
>
> echo "$theHour:$theMinute:$theSecond $dn";
> ?>
>
> =A0 =A0 =A0 =A0It works great except for one small detail. =A0If the time=
is 3:04:02,
> it is displayed as 3:4:2 which, of course, is very odd looking. =A0So I
> corrected it as follows:
>
> > =A0 =A0 =A0 =A0date_default_timezone_set('America/Los_Angeles');
> =A0 =A0 =A0 =A0$theTimeIs =3D getdate(time());
> =A0 =A0 =A0 =A0 =A0 =A0$theHour =3D $theTimeIs['hours'];
> =A0 =A0 =A0 =A0 =A0 =A0if (strlen ($theTimeIs['minutes']) < 2) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$theMinute=
=3D "0" . $theTimeIs['minutes'];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} else {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$theMinute=
=3D $theTimeIs['minutes'];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0 =A0if (strlen ($theTimeIs['seconds']) < 2) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$theSecond=
=3D "0" . $theTimeIs['seconds'];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} else {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$theSecond=
=3D $theTimeIs['seconds'];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0if($theHour > 12){
> =A0 =A0 =A0 =A0 =A0 =A0$theHour =3D $theHour - 12;
> =A0 =A0 =A0 =A0 =A0 =A0$dn =3D "PM";
> =A0 =A0 =A0 =A0} else {
> =A0 =A0 =A0 =A0 =A0 =A0$dn =3D "AM";
> =A0 =A0 =A0 =A0}
>
> echo "$theHour:$theMinute:$theSecond $dn";
> ?>
>
> =A0 =A0 =A0 =A0It 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: Displaying 2 digit minutes/seconds

am 20.08.2009 23:38:40 von Ashley Sheridan

On Thu, 2009-08-20 at 14:27 -0700, sono-io@fannullone.us wrote:
> Hi all,
>
> I'm using this code to display the current time for our location on
> our website:
>
> > 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:
>
> > 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: Displaying 2 digit minutes/seconds

am 21.08.2009 00:49:08 von 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: Displaying 2 digit minutes/seconds

am 21.08.2009 00:52:51 von 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: Displaying 2 digit minutes/seconds

am 21.08.2009 02:12:17 von Daevid Vincent

> -----Original Message-----
> From: sono-io@fannullone.us [mailto:sono-io@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