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 :
- given "datetime" datetime of first "year"
- given "datetime" datetime of first "month"
- given "datetime" datetime of first "week"
- given "datetime" datetime of first "day"
- given "datetime" datetime of first "hour"
- given "datetime" datetime of first "minute"
for date time 2016-01-10 10:30 results :
- year : 2016-01-01 00:00
- month : 2016-01-01 00:00
- week : 2016-01-04 00:00
- day : 2016-01-10 00:00
- hour : 2016-01-10 10:00
- 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
Post a Comment