SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question any function to get the day of a date when date is extract from db?

    is it possible / any function to get the day of a date when date in format yyy-MM-dd is extraced from database in the while loop?

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting timestamp

    First of all convert your date to the *ix time stamp and you can get whatever you want.
    PHP Code:
        $dbdate '2006-12-18';
        list(
    $y$m$d) = split('-',$dbdate);
        
    $timestamp mktime(000$m$d$y);
        echo 
    'Current day = ' date('l F d, Y',$timestamp); 
    hope that it will help you. now its your job to create a function as you like.

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did u mean just extracting the day part from the datetime field of the database? why go to such exorbitant length when MySQL provides some native functions.
    Lets say database field `JoinDate` contains this value: 2006-12-18

    Code:
     select date_format(JoinDate, '%d') from employee Where EmployeeID=5;
    >18
    
    select dayname(JoinDate) from employee Where EmployeeID=5;
    >Monday
    
    select day(JoinDate) from employee Where EmployeeID=5;
    >18
    Even when u want to extract unix timestamp u can use mysql function like:

    Code:
    select UNIX_TIMESTAMP(JoinDate) from employee Where EmployeeID=5;
    will give the timestamp of midnight(exact 12 AM) of that day. now use date() function from php to parse it in whatever way you like.

    even if you have extracted date in format yyy-MM-dd, u can use php function strtotime() to convert it to unix time stamp which again can be formatted in your desired format with date() function.
    for example:

    PHP Code:
    $dbdate '2006-12-18';
    print 
    date('d'strtotime($dbdate)); 

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    i'm gonna guess that "get the day of a date" means the day of the week

    in that case, use the mysql DAYOFWEEK function

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for all the replies...
    yup, i wanna find the day of the date extracted..

    eg. date= 2006-12-18, day=Monday

  6. #6
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cathie
    thanks for all the replies...
    yup, i wanna find the day of the date extracted..

    eg. date= 2006-12-18, day=Monday
    If so, the second query that i wrote is going to work for you.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kailash Badu,
    thanks again!!!

    mmm... after refering to your suggestion...
    i'm trying on the code below...

    PHP Code:
    $day=dayname($row['date']);
    echo 
    $day
    then i get an error:

    Fatal error: Call to undefined function: dayname() in c:\phpdev\www\view.php on line 61

    actually can i use it in this way?

  8. #8
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No u can't. dayname() is a mysql function and not a PHP function. you must use that function in your query.

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kailash Badu View Post
    No u can't. dayname() is a mysql function and not a PHP function. you must use that function in your query.
    ok, learnt something new for me today...

    thanks a lot, Kailash Badu!

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how if i want to convert an extracted data "2006-12-19" to "19 December"?

  11. #11
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    print date('j F'strtotime('2006-12-19'));
    ?>

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kailash Badu View Post
    PHP Code:
    <?php

    print date('j F'strtotime('2006-12-19'));
    ?>
    thanks, you really helpful!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •