Jump to content

[SOLVED] Getting the right date


Brian W

Recommended Posts

:facewall:

I need to get the date of last Friday, unless today is Friday in which case get today's date, as a column value. Can't seem to get a grasp on the STR_TO_DATE function or I'm barking up the tree entirely.

SELECT STR_TO_DATE('Friday','%W %M %Y') as `Date` FROM

 

Any help is appreciated. Thanks

 

I wish I could use php to make this date, to bad in this situation I have to some how pass the date already correct.

Link to comment
https://forums.phpfreaks.com/topic/168336-solved-getting-the-right-date/
Share on other sites

hmm, interesting idea. I like it, but it doesn't work.

the - 7 gets whatever today is - 7 days (same day of the week).

I can use some conditional statements if I can figure out how do do something like

..., NOW() + INTERVAL (7 - DAYOFWEEK( NOW() ) DAY

if the day was monday (2), it would work like

7(days in week) - 2 (monday) = 5

2(today) - 5 =  - 3

NOW() + -3 = 3 days ago which from Monday is Friday I think

Right, right.  My mistake.

 

IF( 
  DAYOFWEEK( NOW() )=6, 
  NOW(), 
  CASE DAYOFWEEK( NOW() )
    WHEN 1 THEN NOW() - INTERVAL 2 DAY
    WHEN 2 THEN NOW() - INTERVAL 3 DAY
    WHEN 3 THEN NOW() - INTERVAL 4 DAY
    WHEN 4 THEN NOW() - INTERVAL 5 DAY
    WHEN 5 THEN NOW() - INTERVAL 6 DAY
    WHEN 7 THEN NOW() - INTERVAL 1 DAY
  END 
) AS `MyValue

 

Maybe?  I dunno!  I stopped using MySQL years ago.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.