Re: Reverse of date("w")

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Chris Ditty wrote:

> Another way to do it would be to store the unix epoch and then just
> get the
> weekday name from that?  More overhead than Travis's idea, but just as
> good
> and you could possibly use the date/time later on.


I use the DATETIME fieldtypes in MySQL, same idea... 100% agreed that
keeping that timestamp for later use is a good idea.

For Kevin's original question.. Here is the MySQL function to get that
short weekday name off of a DATETIME column:

Table with datetime column:

mysql> SELECT id,effectivetime
    -> FROM event
    -> WHERE employee_id ='1001';
+-----+---------------------+
| id  | effectivetime       |
+-----+---------------------+
| 184 | 2006-09-18 18:50:25 |
| 182 | 2006-09-17 23:12:17 |
| 178 | 2006-09-12 21:59:44 |
+-----+---------------------+
3 rows in set (0.00 sec)



Query to get 'weekday':

mysql> SELECT id,effectivetime,DATE_FORMAT(effectivetime,"%a") AS weekday
    -> FROM event
    -> WHERE employee_id = '1001';
+-----+---------------------+---------+
| id  | effectivetime       | weekday |
+-----+---------------------+---------+
| 184 | 2006-09-18 18:50:25 | Mon     |
| 182 | 2006-09-17 23:12:17 | Sun     |
| 178 | 2006-09-12 21:59:44 | Tue     |
+-----+---------------------+---------+
3 rows in set (0.00 sec)

Those DATETIME columns can do some other neat things too:

SELECT * FROM events WHERE starttime BETWEEN '2006-09-18' AND '2006-09-28'
Gives me a list of events happening in a certain period...

SELECT * FROM events WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) < starttime
Gets you the last 30 days without needing to make those '2006-09-18'
style strings in PHP code.

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
They should all work on DATETIME type columns, storing seconds since
epoch doesn't quite give you that (you could work it in pretty easily
I'm sure, why not use the native column type!)

Cheers,
Travis

>
> On 9/18/06, Travis Doherty <travis@xxxxxxxxxxxxx> wrote:
>
>>
>> Kevin Murphy wrote:
>>
>> > Not really. If it were always "today" that would work, but in this
>> > case, I was thinking of storing a day of the week in a database
>> > ("3"), and then display the info based on that digit. So assuming
>> > that the number was in fact 3, then:
>> >
>> > echo date("D","3");
>> >
>> > Would return "Wed".
>> >
>> > Is there any function like that? Oh, and it has to run on PHP 4.
>> >
>>
>> Any reason you wouldn't write it yourself?
>>
>> <?php
>> function getDayFromInteger($integer)
>> {
>>
>>    $days = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
>>
>>    if (isset($days[$integer]))
>>    {
>>       return $days[$integer];
>>    }
>>
>>    return false;
>>
>> }
>> ?>
>>
>> -- 
>> 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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux