php - How to get start date of child time frame within parent time frame -


i know exact date of beginning of month need know when first week of month begins , week can start outside of month.

i can - find weekday when "month" starts , subtract number of days beginning of "week" :

$weekday = d('w', $monthstarttimestamp); $weekstart = $monthstarttimestamp - strtotime('1 day', 0) * (7 - $weekday); 

question :

is there way make calculation more generic time frames not know in advance?

possible use cases :

  1. given "datetime" datetime of first "year"
  2. given "datetime" datetime of first "month"
  3. given "datetime" datetime of first "week"
  4. given "datetime" datetime of first "day"
  5. given "datetime" datetime of first "hour"
  6. given "datetime" datetime of first "minute"

for date time 2016-01-10 10:30 results :

  1. year : 2016-01-01 00:00
  2. month : 2016-01-01 00:00
  3. week : 2016-01-04 00:00
  4. day : 2016-01-10 00:00
  5. hour : 2016-01-10 10:00
  6. minute : 2016-01-10 10:30

p. s. of questions here specify time frame required, e.g. "how beginning of year", not answer how ant time frame.

here's idea in pseudocode:

  • you have hardest case week
  • for rest: convert date format like: yyyymmddhhmm, depending on $interval change right x characters 0:

`

$int2char = array(     'year' => 8,     'month' => 6,     'day' => 4,     'hour' => 2,     'minute' => 0 );  $interval = 'month'; $str = date("ymdhi", $timestamp); $zeros = $int2char[$interval]; $d = substring($str, 0, 12-$zeros) . substring("00000000", 0, $zeros); $date = datetime::createfromformat('ymdhi', $d); echo $date->format('y-m-d h:i'); 

`


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -