PHP, extracting the day name of a selected date?

  • MISC/A++
  • Expert
  • Expert
  • User avatar
  • Posts: 508
  • Loc: UnderGround

Post 3+ Months Ago

I use a javascript popup calendar which outputs the result into a textbox, the next step is to post to get the date selected.

For example, if i select 2007-11-05 from the calendar how can i find the day of the week?

For example 2007-11-05 is on Monday.
  • Nebulous
  • Beginner
  • Beginner
  • Nebulous
  • Posts: 59
  • Loc: Hampshire, UK

Post 3+ Months Ago

I haven't had the time, nor access to the facilities to test this at the moment, but from what I can see it should work without any problems...


PHP Code: [ Select ]
 
$date = '2007/10/31';
 
$weekday = date('l', strtotime($date)); // note: first arg to date() is lower-case L
 
echo $weekday; // SHOULD display Wednesday
 
 
  1.  
  2. $date = '2007/10/31';
  3.  
  4. $weekday = date('l', strtotime($date)); // note: first arg to date() is lower-case L
  5.  
  6. echo $weekday; // SHOULD display Wednesday
  7.  
  8.  


The script is not dependant on the input structure of the date, providing it is valid, so you can customize it to your needs.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Nebulous wrote:
I haven't had the time, nor access to the facilities to test this at the moment, but from what I can see it should work without any problems...


PHP Code: [ Select ]
$date = '2007/10/31';
$weekday = date('l', strtotime($date)); // note: first arg to date() is lower-case L
echo $weekday; // SHOULD display Wednesday
 
  1. $date = '2007/10/31';
  2. $weekday = date('l', strtotime($date)); // note: first arg to date() is lower-case L
  3. echo $weekday; // SHOULD display Wednesday
  4.  


The script is not dependant on the input structure of the date, providing it is valid, so you can customize it to your needs.


That displays Wednesday for every date.
  • MISC/A++
  • Expert
  • Expert
  • User avatar
  • Posts: 508
  • Loc: UnderGround

Post 3+ Months Ago

PHP Code: [ Select ]
 
echo date('l', strtotim($date));
 
 
  1.  
  2. echo date('l', strtotim($date));
  3.  
  4.  


Thanks guys.
  • Abdullah62
  • Born
  • Born
  • Abdullah62
  • Posts: 1

Post 3+ Months Ago

to all

guys thanks for your help.

I have faced the same problem and you have helped me

I want to edit to what you have wrote instead of using the method strtotim($date)) which change the string to date for those who use a date varible, they don't need to use the method strtotim(),just past the date variable alone

so the end result will be like this
echo date('l', $date));

now you will get the date names
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

PHP Code: [ Select ]
<?php
 
// Create a new instance
$today = new DateTime('2015-11-05');
 
// Look a year into the future for example sake
$today->modify('+1 year 12 days');
 
// Display full day name
echo $today->format('l') . PHP_EOL; // lowercase L
 
// Display three-letter day name
echo $today->format('D') . PHP_EOL;
 
?>
  1. <?php
  2.  
  3. // Create a new instance
  4. $today = new DateTime('2015-11-05');
  5.  
  6. // Look a year into the future for example sake
  7. $today->modify('+1 year 12 days');
  8.  
  9. // Display full day name
  10. echo $today->format('l') . PHP_EOL; // lowercase L
  11.  
  12. // Display three-letter day name
  13. echo $today->format('D') . PHP_EOL;
  14.  
  15. ?>


Code: [ Select ]
joebert@frankenstien:~/Desktop$ php -f _.php
Thursday
Thu
joebert@frankenstien:~/Desktop$
  1. joebert@frankenstien:~/Desktop$ php -f _.php
  2. Thursday
  3. Thu
  4. joebert@frankenstien:~/Desktop$

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 55 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.