42973

get the last thursday before a date in PHP

Question:

I know there is similar post about how to get the last Thursday in PHP but I don't want to have the last Thursday compare to the current date but the last Thursday compare to a given date.

For example I have a date dd/mm/yyyy and I want the Thursday before this date. The input is a String ( the format of the string yymmdd) that I want to parse to get the Thursday before this date.

Thanks for your help

Answer1:

//Assumes it's strtotime parsable, you may need to insert // slashes with your given format eg (and use 4 digit years) $given=strtotime($dtstring); //It's just that easy ;) $thuBefore=strtotime("last thursday",$given);

Note that this will always get <em>last</em> thursday, meaning if the given date is a Thursday, it'll report 7 days earlier (but if the date's a Friday it'll only report one day earlier).

Answer2:

$day = date('w', $yourTime); $time = $yourTime - ($day > 4 ? ($day - 4) : ($day + 7 - 4)) * 3600 * 24;

Where both $yourTime and $time are Unix-timestamps.

<strong>Edit:</strong> @Rudu's solution is way more simple, you should stick with that one :)!

Recommend

  • How to convert string of type YY/MM/DD to java.util.Date?
  • Regular Expression to generate a string
  • Reading DBF File Structure in VB.Net as binary file
  • find cout of specific day between two date php without using any loop?
  • Converting mysql date from one format to another
  • strtotime() converts a non existing date to another date
  • Show a link in fpdf
  • Is there an existing gem or script that converts numbers to comp-3/packed decimal format?
  • In Python, how should one extract the second-last directory name in a path?
  • Python Floating Point Formatting
  • Custom URI routing by query string with CodeIgniter?
  • staticfiles and STATIC_URL with virrtualenv - django
  • Comma within fields in CSV file -import to DB using SSIS
  • Separate float into digits
  • How to search a CSV file with php by checking if a date falls between 2 ranges
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • Android changing fragment order inside FragmentPagerAdapter
  • How can I sort a a table with VBA with given text condition?
  • How do I open a C file with a relative path?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Master page gives error
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • AT Commands to Send SMS not working in Windows 8.1
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • Sorting a 2D array using the second column C++
  • Python/Django TangoWithDjango Models and Databases