Skip to content

Instantly share code, notes, and snippets.

@BKeanu1989
Created May 6, 2019 22:26
Show Gist options
  • Save BKeanu1989/fa599e31fb124eeebd1d78a86b7730ae to your computer and use it in GitHub Desktop.
Save BKeanu1989/fa599e31fb124eeebd1d78a86b7730ae to your computer and use it in GitHub Desktop.
date from Strings with fixed start & end date
<?php
$festivalStart = '2019-07-29';
$festivalEnd = '2019-08-04';
$germanDays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'];
$englishDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
function get_begin_N_end_from_time_span($timeSpanString)
{
preg_match('/(?P<begin>\w+).{0,3} (?P<end>\w+)/', $timeSpanString, $matches);
return $matches;
}
$periodSingle = 'Donnerstag';
function handle_single_period($periodSingle)
{
$germanDays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'];
$englishDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
$festivalStart = '2019-07-29';
$festivalEnd = '2019-08-04';
$translatedPeriod = str_replace($germanDays, $englishDays, $periodSingle);
$startObject = new DateTime($festivalStart);
$festivalStartDay = $startObject->format('l');
$indexPeriodSingle = array_search($translatedPeriod, $englishDays);
$indexFestivalStartDay = array_search($festivalStartDay, $englishDays);
$difference = $indexPeriodSingle - $indexFestivalStartDay;
$periodSingleDateObject = $startObject->modify("+{$difference} days");
return $periodSingleDateObject->format('Y-m-d');
}
// echo handle_single_period($periodSingle);
$periodTimeSpan = 'Montag - Sonntag';
function handle_time_span($timeSpanString)
{
$timeSpan = get_begin_N_end_from_time_span($timeSpanString);
$germanDays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'];
$englishDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
$festivalStart = '2019-07-29';
$festivalEnd = '2019-08-04';
$endDateObject = new DateTime($festivalEnd);
$festivalEndDay = $endDateObject->format('l');
$translatedEndDay = str_replace($germanDays, $englishDays, $timeSpan['end']);
$indexPeriodEnd = array_search($translatedEndDay, $englishDays);
$indexFestivalEnd = array_search($festivalEndDay, $englishDays);
$differenceEnds = $indexFestivalEnd - $indexPeriodEnd;
$userEndDate = $endDateObject->modify("-{$differenceEnds} days");
$startDateObject = new DateTime($festivalStart);
$festivalStartDay = $startDateObject->format('l');
$translatedStartDay = str_replace($germanDays, $englishDays, $timeSpan['begin']);
$indexPeriodStart = array_search($translatedStartDay, $englishDays);
$indexFestivalStart = array_search($festivalStartDay, $englishDays);
$differenceStarts = $indexPeriodStart - $indexFestivalStart;
$userStartDate = $startDateObject->modify("+{$differenceStarts} days");
return ['startDate' => $userStartDate->format('Y-m-d'), 'endDate' => $userEndDate->format('Y-m-d')];
}
var_dump(handle_time_span($periodTimeSpan));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment