CSS & Web Design Forum  

Welcome to the CSS & Web Design Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
WM Media Banner

Supporters
Pos Software - POS hardware and Software to track, control and manage your inventory.
Web Templates - BoxedArt is the most reliable source for unlimited template downloads.

Social Bookmarking
WL Marketing - Your #1 source for building links

Go Back   CSS & Web Design Forum > Programming > Programming > PHP
Reply
 
Thread Tools Search this Thread Display Modes
(#1)
Old
Bosky, Dave
Guest
 
Default convert numerical day of week - 05-23-2007, 07:00 AM

How can I convert the numerical day of week to the string version?

Example, if the day of the week is 1 I would like to print out 'Sunday'.



Thanks,

Dave




************************************************** ********************
HTC Disclaimer: The information contained in this message may be privileged and confidential and protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible fordelivering this message to the intended recipient, you are hereby notifiedthat any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you.
************************************************** ********************


Reply With Quote
(#2)
Old
Daniel Brown
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

Do you mean by using the date() function?


On 5/22/07, Bosky, Dave <Dave.Bosky@htcinc.net> wrote:
>
> How can I convert the numerical day of week to the string version?
>
> Example, if the day of the week is 1 I would like to print out 'Sunday'.
>
>
>
> Thanks,
>
> Dave
>
>
>
>
> ************************************************** ********************
> HTC Disclaimer: The information contained in this message may be
> privileged and confidential and protected from disclosure. If the reader of
> this message is not the intended recipient, or an employee or agent
> responsible for delivering this message to the intended recipient, you are
> hereby notified that any dissemination, distribution or copying of this
> communication is strictly prohibited. If you have received this
> communication in error, please notify us immediately by replying to the
> message and deleting it from your computer. Thank you.
> ************************************************** ********************
>
>



--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

Reply With Quote
(#3)
Old
Richard Davey
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

Hi Dave,

Tuesday, May 22, 2007, 5:46:38 PM, you wrote:

> How can I convert the numerical day of week to the string version?
> Example, if the day of the week is 1 I would like to print out 'Sunday'.


$days = array(1 => 'Sunday', 2 => 'Monday', 3 => 'Tuesday', etc ...);

then just

$today = $days[1]; // Sunday
$today = $days[3]; // Tuesday

Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"
Reply With Quote
(#4)
Old
Greg Donald
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

On 5/22/07, Bosky, Dave <Dave.Bosky@htcinc.net> wrote:
> How can I convert the numerical day of week to the string version?
>
> Example, if the day of the week is 1 I would like to print out 'Sunday'.


$days = array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday' );

$day = 1;

echo $days[ $day - 1 ];


--
Greg Donald
http://destiney.com/
Reply With Quote
(#5)
Old
Daniel Brown
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

In that case, one simple way (though not the most economical way, and
really hacky, reinventing the wheel in the process) would be to create a
function like so:

<?
function num2day($num) {
if($num == "1") {
$day = "Sunday";
} elseif($num == "2") {
$day = "Monday";
} elseif($num == "3") {
$day = "Tuesday";
} elseif($num == "4") {
$day = "Wednesday";
} elseif($num == "5") {
$day = "Thursday";
} elseif($num == "6") {
$day = "Friday";
} elseif($num == "7") {
$day = "Saturday";
}
return $day;
}
?>

On 5/22/07, Bosky, Dave <Dave.Bosky@htcinc.net> wrote:
>
> I'm not really sure. I've got a numerical interpretation of the day of
> the week in a table and I need to print out what day of the week it
> translates to.
>
> I tried using date_format in the MySQL query but it didn't seem to work.
>
>
>
>
> ------------------------------
>
> *From:* Daniel Brown [mailtoarasane@gmail.com]
> *Sent:* Tuesday, May 22, 2007 12:50 PM
> *To:* Bosky, Dave
> *Cc:* php-general@lists.php.net
> *Subject:* Re: [php] convert numerical day of week
>
>
>
>
> Do you mean by using the date() function?
>
> On 5/22/07, *Bosky, Dave* <Dave.Bosky@htcinc.net> wrote:
>
> How can I convert the numerical day of week to the string version?
>
> Example, if the day of the week is 1 I would like to print out 'Sunday'.
>
>
>
> Thanks,
>
> Dave
>
>
>
>
> ************************************************** ********************
> HTC Disclaimer: The information contained in this message may be
> privileged and confidential and protected from disclosure. If the reader of
> this message is not the intended recipient, or an employee or agent
> responsible for delivering this message to the intended recipient, you are
> hereby notified that any dissemination, distribution or copying of this
> communication is strictly prohibited. If you have received this
> communication in error, please notify us immediately by replying to the
> message and deleting it from your computer. Thank you.
> ************************************************** ********************
>
>
>
>
> --
> Daniel P. Brown
> [office] (570-) 587-7080 Ext. 272
> [mobile] (570-) 766-8107
>




--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

Reply With Quote
(#6)
Old
Greg Donald
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

On 5/22/07, Daniel Brown <parasane@gmail.com> wrote:
> <?
> function num2day($num) {
> if($num == "1") {
> $day = "Sunday";
> } elseif($num == "2") {
> $day = "Monday";
> } elseif($num == "3") {
> $day = "Tuesday";
> } elseif($num == "4") {
> $day = "Wednesday";
> } elseif($num == "5") {
> $day = "Thursday";
> } elseif($num == "6") {
> $day = "Friday";
> } elseif($num == "7") {
> $day = "Saturday";
> }
> return $day;
> }
> ?>


PHP does automatic type conversions, even if $num is passed in as a
string all those quotes around the integers are not necessary. There
is no variable interpolation required in the day names so all those
double quotes should be single quotes. A function call seems rather
heavy when a global array can be indexed more easily. Even a switch
statement would be an improvement.


--
Greg Donald
http://destiney.com/
Reply With Quote
(#7)
Old
Daniel Brown
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

On 5/22/07, Greg Donald <gdonald@gmail.com> wrote:
>
> On 5/22/07, Daniel Brown <parasane@gmail.com> wrote:
> > <?
> > function num2day($num) {
> > if($num == "1") {
> > $day = "Sunday";
> > } elseif($num == "2") {
> > $day = "Monday";
> > } elseif($num == "3") {
> > $day = "Tuesday";
> > } elseif($num == "4") {
> > $day = "Wednesday";
> > } elseif($num == "5") {
> > $day = "Thursday";
> > } elseif($num == "6") {
> > $day = "Friday";
> > } elseif($num == "7") {
> > $day = "Saturday";
> > }
> > return $day;
> > }
> > ?>

>
> PHP does automatic type conversions, even if $num is passed in as a
> string all those quotes around the integers are not necessary. There
> is no variable interpolation required in the day names so all those
> double quotes should be single quotes. A function call seems rather
> heavy when a global array can be indexed more easily. Even a switch
> statement would be an improvement.
>
>
> --
> Greg Donald
> http://destiney.com/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

You may have noticed my disclaimer at the head of the message. It was
there for just that reason....

--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

Reply With Quote
(#8)
Old
Robert Cummings
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

On Tue, 2007-05-22 at 12:46 -0400, Bosky, Dave wrote:
> How can I convert the numerical day of week to the string version?
>
> Example, if the day of the week is 1 I would like to print out 'Sunday'.


I saw a bunch of bad examples given to you that completely ignore any
available locale information... so here's a better version:

<?php

function dayIdToName( $id )
{
static $days = null;
if( $days === null )
{
$control = gmmktime( 12, 1, 1, 5, 20, 2007 );
for( $i = 0; $i < 7; $i++ )
{
$days[$i] = gmdate( 'l', $control + ($i * 24 * 60 * 60) );
}
}

return isset( $days[$id] ) ? $days[$id] : null;
}

?>

If you want Monday to be the first day of the week, just use a date for
gmmktime() that falls on a Monday.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
(#9)
Old
Greg Donald
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

On 5/22/07, Robert Cummings <robert@interjinn.com> wrote:
> I saw a bunch of bad examples given to you that completely ignore any
> available locale information... so here's a better version:


I seem to have missed the part of the question where it said
considering locale was important.


--
Greg Donald
http://destiney.com/
Reply With Quote
(#10)
Old
Robert Cummings
Guest
 
Default Re: [PHP] convert numerical day of week - 05-23-2007, 07:00 AM

On Tue, 2007-05-22 at 14:32 -0400, Daniel Brown wrote:
> On 5/22/07, Robert Cummings <robert@interjinn.com> wrote:
> >
> > On Tue, 2007-05-22 at 12:58 -0500, Greg Donald wrote:
> > > On 5/22/07, Robert Cummings <robert@interjinn.com> wrote:
> > > > I saw a bunch of bad examples given to you that completely ignore any
> > > > available locale information... so here's a better version:
> > >
> > > I seem to have missed the part of the question where it said
> > > considering locale was important.

> >
> > Nothing said it was important, but why implement a half-assed solution
> > when you can implement a superior solution in as much time?
> >
> > I'll accept ignorance and sloppiness as reasons... albeit not good
> > reasons.

>
> My message was in direct response to the user's request.... I think that
> kinda' got lost somewhere in this thread....


I didn't respond to your message. I responded directly to the OP's
original post.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
Reply

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Hosting at Triple.com
vBulletin Skin developed by: vBStyles.com

Printing Company  Link Building Services