I have a system where I manage multiple students, each with their own timezone, class days, and class times. However, my teachers are in a different timezone. I need to convert each student's class times to match the teacher's timezone so that scheduling and communication are synchronized.
For example:
Student A is in "Timezone A" and has a class on Monday at 10:00 AM. Student B is in "Timezone B" and has a class on Monday at 3:00 PM. The teacher is in "Timezone C." I want to convert Student A's class time to match "Timezone C" (teacher's timezone), which would be Monday at 2:00 AM. Similarly, Student B's class time should be converted to Monday at 7:00 AM in "Timezone C."
What's the best approach to achieve this conversion in PHP? Are there any libraries or methods that can help streamline this process?
Any guidance or code examples would be greatly appreciated.
Thank you!
protected function getDaySchedule($weeklyDaysJson, $targetDay, $targetTimeZone)
{
$schedule = json_decode($weeklyDaysJson);
if (!empty($schedule)) {
foreach ($schedule as $entry) {
if ($entry->day === $targetDay) {
$time = $entry->time;
$duration = $entry->duration;
return "$time - $duration minutes";
}
}
} else {
return '';
}
}
Using Yajra Datatable:
->addColumn('monday', function ($row) {
return $this->getDaySchedule($row->weekly_days, 'monday');
})
[![enter image description here](https://i.sstatic.net/YFWDM.png)](https://i.sstatic.net/YFWDM.png)
This function accepts the week's JSON schedule, the target day, the student's time zone, and the target teacher's time zone as input. It then searches the schedule for the target day, converts the class start time to the teacher's time zone, and includes the duration. If the desired date cannot be found, it returns an empty string.
protected function getDaySchedule($weeklyDaysJson, $targetDay, $time_zone, $targetTimeZone)
{
$schedule = json_decode($weeklyDaysJson);
if (!empty($schedule)) {
foreach ($schedule as $entry) {
if ($entry->day === $targetDay) {
$time = $entry->time;
$duration = $entry->duration;
$classStartTime = Carbon::createFromTime($time[0], $time[1], 0, $time_zone);
$studentTime = $classStartTime->copy()->tz($targetTimeZone);
return "{$studentTime->format('H:i')} - {$duration} minutes";
}
}
}
return '';
}
I use this to yajra by adding column:
->addColumn('monday', function ($row) {
$tTimezone = $row->teacher->time_zone;
$time_zone = $row->zoomStudent->time_zone;
return $this->getDaySchedule($row->weekly_days, 'monday', $time_zone, $tTimezone);
})
Thank You for your help! keep growing