Getting string form of date value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Getting string form of date value

    I need the string form of a date minus 6 days. This is driving me crazy

    Code:
    $a_Date_String = '20140809';
    
    //this works fine
    $Week_Ending = date ("Y-m-d", strtotime($a_Date_String));
    
    $Week_Begin = new DateTime($a_Date_String);
    $Week_Begin->sub(new DateInterval('P6D'));
    
    /*
    I now have a string for $Week_Ending "2014-08-09", 
    but the $Week_Begin is a date object
    
    [B]How do I extract "2014-08-03" from the $Week_Begin[/B]? 
    
    I want to use the string value in my query on the MySQL table like this. 
    */
    $sql = 'select '.$cSelect.' from '.$cFrom.' where workDay between " '.[B]$Week_Begin[/B].'" and "'.[B]$Week_Ending[/B].'" ' order by '.$cOrderBy;
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    Ok after much testing and searching on the net, I finally found the not so intuitive solution
    Code:
    $a_Date_String = '20140809';
     
    $Week_Ending = date ("Y-m-d", strtotime($a_Date_String));
     
    $Week_Begin = new DateTime($a_Date_String);
    $Week_Begin->sub(new DateInterval('P6D'));
    
    //you would think they would have a timetostr function instead of 'format'
    [B]$Week_Begin = $Week_Begin->format('Y-m-d');[/B]

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      look like it’s for a DB. you could also make the calculation there.

      Comment

      Working...