473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Date math and string conversion

Claus Mygind
571 512MB
I am passing my app a string "20140920" (last day of the week)

I want to create two date strings to use on a MySQL query where I retrieve all records for the week.

The two variables should be the first and last day of the week like this.

Expand|Select|Wrap|Line Numbers
  1. $end = "20140920"
  2. $start = "20140914"
I have tried a number of combinations of code to get this but I'm just stuck. A simple example would be appreciated.
Sep 26 '14 #1

✓ answered by Claus Mygind

Some times it's the little things that get you. Was passing " around the string.
Expand|Select|Wrap|Line Numbers
  1. //setup date range
  2. /*
  3. ---------------------------------
  4. end of pay period
  5. already a string in format needed
  6. no action required
  7. ---------------------------------
  8. */
  9. $lastDay = $_REQUEST['WeekEnding'];
  10.  
  11.  
  12. /*
  13. ----------------------------------------------
  14. start of pay period
  15. make a date object with "new DateTime($var)"
  16. ----------------------------------------------
  17. */
  18. $x = new DateTime($_REQUEST['WeekEnding']);
  19.  
  20. /*
  21. --------------------------------------
  22. do math operation on date object 
  23. using $var->sub() to subtract or 
  24. $var->add to add days to date object
  25. --------------------------------------
  26. */
  27. $x->sub(new DateInterval('P6D'));
  28.  
  29. /*
  30. ------------------------------------
  31. convert date object 
  32. back into a string with format()
  33. ------------------------------------
  34. */
  35. $firstDay = $x->format('Ymd');
  36.  
  37.  

1 1294
Claus Mygind
571 512MB
Some times it's the little things that get you. Was passing " around the string.
Expand|Select|Wrap|Line Numbers
  1. //setup date range
  2. /*
  3. ---------------------------------
  4. end of pay period
  5. already a string in format needed
  6. no action required
  7. ---------------------------------
  8. */
  9. $lastDay = $_REQUEST['WeekEnding'];
  10.  
  11.  
  12. /*
  13. ----------------------------------------------
  14. start of pay period
  15. make a date object with "new DateTime($var)"
  16. ----------------------------------------------
  17. */
  18. $x = new DateTime($_REQUEST['WeekEnding']);
  19.  
  20. /*
  21. --------------------------------------
  22. do math operation on date object 
  23. using $var->sub() to subtract or 
  24. $var->add to add days to date object
  25. --------------------------------------
  26. */
  27. $x->sub(new DateInterval('P6D'));
  28.  
  29. /*
  30. ------------------------------------
  31. convert date object 
  32. back into a string with format()
  33. ------------------------------------
  34. */
  35. $firstDay = $x->format('Ymd');
  36.  
  37.  
Sep 26 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: John van Terheijden | last post by:
Hi. I'm trying to make a conversion algorithm that colors even and odd words in a HTML string with <div> tags. // input text with all sorts of HTML tags and whitespace $str = "<h1>Title...
3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
1
by: Alex | last post by:
I have two SQL statements: Statement #1 SELECT CARRIER_REFERENCE_NUMBER, CURRENT_FLEET_SIZE, trunc(Fleet_Size_Effective_From_Date), trunc(Fleet_Size_Effective_To_Date) FROM...
8
by: alex | last post by:
i have a string "dasfk" stored in a variable of type string and i want to convert it to const _bstr_t, anybody know? Posted Via Usenet.com Premium Usenet Newsgroup Services...
3
by: andrew | last post by:
Hi: I am already using TreeMap to massage records in my export file such that each record has a unique key combination ( LastName + FirstName + Member Key) . Thus I am sorting the records by...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
11
by: jwf | last post by:
I am writing a NON MFC C++ application (Plug-in for a 3rd party DB system). The application is designed to synchronise data between MS Outlook and the DB system. I am using smart pointers for the...
10
by: Andrew | last post by:
Having trouble with some date computations. I need to get the date of a specific day within a month - the 1st Thursday or the 2nd Monday, etc. I found an article (...
2
by: Chris H | last post by:
Hi, I'm trying to concatenate a Description (nchar(100)) and Date (datetime) as Description and my initial effort was just "...description+' '+open_date as description..." which throws a date/...
2
by: Saeed Amrollahi | last post by:
Dear All Hi I usually use the following function for built-in types to std::string conversion: // convert a type (typically built-in type) to std::string template<class T> inline std::string...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.