retrieve the day of the week

This is a discussion on retrieve the day of the week within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi i am trying to find a way to retrieve the day of the week from a mysql date. example ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-12-2008
Kram Techie
 
Posts: n/a
Default retrieve the day of the week

Hi

i am trying to find a way to retrieve the day of the week from a mysql date.
example 11 Mar 2008 22:32:06
should equate to Tue, or Tuesday

Googled but no Joy. many online examples but i need a hint or some source to
work with.

Mark


Reply With Quote
  #2 (permalink)  
Old 03-12-2008
ZeldorBlat
 
Posts: n/a
Default Re: retrieve the day of the week

On Mar 12, 8:15 am, "Kram Techie" <kramTec...@NOSPAM.ntlworld.com>
wrote:
> Hi
>
> i am trying to find a way to retrieve the day of the week from a mysql date.
> example 11 Mar 2008 22:32:06
> should equate to Tue, or Tuesday
>
> Googled but no Joy. many online examples but i need a hint or some source to
> work with.
>
> Mark


Googling is fine -- but it wouldn't have hurt to look in the MySQL
manual, either:

<http://dev.mysql.com/doc/refman/5.0/en/date-and-time-
functions.html#function_dayname>
Reply With Quote
  #3 (permalink)  
Old 03-12-2008
Alred Wallace
 
Posts: n/a
Default Re: retrieve the day of the week


"Kram Techie" <kramTechie@NOSPAM.ntlworld.com> a écrit dans le message de
news: 9PPBj.10809$qW6.8607@newsfe6-win.ntli.net...
> Hi
>
> i am trying to find a way to retrieve the day of the week from a mysql
> date.
> example 11 Mar 2008 22:32:06
> should equate to Tue, or Tuesday
>
> Googled but no Joy. many online examples but i need a hint or some source
> to work with.
>
> Mark
>
>



hi Mark,
first the timestamp is the best way to find day of the week. A timestamp
look like : 3215649870354 (secondes).

I don't know how are stored your data. Let's imagine the worst: in a
string!!
and the month are stored like:jan, mar,feb...
and not like 1 (jan), 2 (feb)...
we will be forced to make an array.
Let's begin the script:

<?
///////////////// input date:
$date = "11 Mar 2008 22:32:06";

///////////// array retrivinbg number of months:
$Months = Array(
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12
);


///////// let's transorm your date in timestamp:
/// let's mlake an array to retrive values of: day, month, year:
$dateArray = explode( " ",$date );
$day = $dateArray[0];
/////// tranform the month like "Mar" in a number (3)
$month = $Months[ $dateArray[1] ];
$year=$dateArray[2];
///// now, we own: $day, $month,$year in numeric format.

// creating timestamp: mktime(hour, minute,second,month,day,year);
/////// in this case the value is:1205193600
$dateTimeStamp = mktime( 1, 0 , 0 , $month , $day , $year );

//////// retreiving the name of the day (see "date function" doc in php
manual)
//// "l" is L lowercase
$TheNameOfTheDay = date( "l",$dateTimeStamp );

echo "date ini:".$date."<br />";
echo "<h1>".$TheNameOfTheDay."</h1>";
echo "timestamp:".$dateTimeStamp;
?>






Reply With Quote
  #4 (permalink)  
Old 03-12-2008
Michael Fesser
 
Posts: n/a
Default Re: retrieve the day of the week

..oO(Kram Techie)

>i am trying to find a way to retrieve the day of the week from a mysql date.
>example 11 Mar 2008 22:32:06


This is not a MySQL date, which would be 2008-03-11 22:32:06 instead.
With that format it's pretty easy to do calculations and manipulations
with MySQL's date and time functions.

Micha
Reply With Quote
  #5 (permalink)  
Old 03-12-2008
Michael Fesser
 
Posts: n/a
Default Re: retrieve the day of the week

..oO(Alred Wallace)

>first the timestamp is the best way to find day of the week. A timestamp
>look like : 3215649870354 (secondes).


The best is to let the database handle that.

>I don't know how are stored your data. Let's imagine the worst: in a
>string!!
>and the month are stored like:jan, mar,feb...
>and not like 1 (jan), 2 (feb)...
>we will be forced to make an array.
>Let's begin the script:
>[...]


Have a look at strtotime().

<?php
$date = '11 Mar 2008 22:32:06';
print date('l', strtotime($date));
?>

Micha
Reply With Quote
  #6 (permalink)  
Old 03-12-2008
Kram Techie
 
Posts: n/a
Default Re: retrieve the day of the week


"Michael Fesser" <netizen@gmx.de> wrote in message
news:2brft39fv1d57bnhhdom9lvs7irsgsp122@4ax.com...
> .oO(Alred Wallace)
>
>>first the timestamp is the best way to find day of the week. A timestamp
>>look like : 3215649870354 (secondes).

>
> The best is to let the database handle that.
>
>>I don't know how are stored your data. Let's imagine the worst: in a
>>string!!
>>and the month are stored like:jan, mar,feb...
>>and not like 1 (jan), 2 (feb)...
>>we will be forced to make an array.
>>Let's begin the script:
>>[...]

>
> Have a look at strtotime().
>
> <?php
> $date = '11 Mar 2008 22:32:06';
> print date('l', strtotime($date));
> ?>
>
> Micha


Hi Micha

that works a treat

Thanks to all who replied

Mark


Reply With Quote
  #7 (permalink)  
Old 03-12-2008
Michael Fesser
 
Posts: n/a
Default Re: retrieve the day of the week

..oO(Kram Techie)

>"Michael Fesser" <netizen@gmx.de> wrote in message
>news:2brft39fv1d57bnhhdom9lvs7irsgsp122@4ax.com.. .
>>
>> <?php
>> $date = '11 Mar 2008 22:32:06';
>> print date('l', strtotime($date));
>> ?>
>>
>> Micha

>
>Hi Micha
>
>that works a treat


It still means that the dates in your DB are stored in the wrong format.
The date column should be of type DATETIME.

Micha
Reply With Quote
  #8 (permalink)  
Old 03-12-2008
Kram Techie
 
Posts: n/a
Default Re: retrieve the day of the week

"Michael Fesser" <netizen@gmx.de> wrote in message
news:lgtft31jkvec591mcnjgob2q1cbhfeab61@4ax.com...
> .oO(Kram Techie)
>
>>"Michael Fesser" <netizen@gmx.de> wrote in message
>>news:2brft39fv1d57bnhhdom9lvs7irsgsp122@4ax.com. ..
>>>
>>> <?php
>>> $date = '11 Mar 2008 22:32:06';
>>> print date('l', strtotime($date));
>>> ?>
>>>
>>> Micha

>>
>>Hi Micha
>>
>>that works a treat

>
> It still means that the dates in your DB are stored in the wrong format.
> The date column should be of type DATETIME.
>
> Micha


Hi Micha

it is DATETIME but for display i have to re-format it.

Mark


Reply With Quote
  #9 (permalink)  
Old 03-12-2008
Michael Fesser
 
Posts: n/a
Default Re: retrieve the day of the week

..oO(Kram Techie)

>it is DATETIME but for display i have to re-format it.


Ah, OK. But then you can let the database return the day of week, which
would be more efficient than doing it in PHP. See DATE_FORMAT() for
details.

http://dev.mysql.com/doc/refman/5.0/...on_date-format

Micha
Reply With Quote
  #10 (permalink)  
Old 03-14-2008
Kram Techie
 
Posts: n/a
Default Re: retrieve the day of the week


"Michael Fesser" <netizen@gmx.de> wrote in message
news:j52gt3tlsg7tv1b3ml0uhi7p0tlh843g9i@4ax.com...
> .oO(Kram Techie)
>
>>it is DATETIME but for display i have to re-format it.

>
> Ah, OK. But then you can let the database return the day of week, which
> would be more efficient than doing it in PHP. See DATE_FORMAT() for
> details.
>
> http://dev.mysql.com/doc/refman/5.0/...on_date-format
>
> Micha


Hi

i used %a to get short day "SELECT DATE_FORMAT('$datetime', '%a')"
works a treat and the other % values will come in handy at a later date.

Mark


Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:34 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0