PHP code example of kachnitel / duration-bundle
1. Go to this page and download the library: Download kachnitel/duration-bundle library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kachnitel / duration-bundle example snippets
return [
// ...
Kachnitel\DurationBundle\DurationBundle::class => ['all' => true],
];
use Kachnitel\DurationBundle\Service\DurationUtil;
// Parse duration strings to seconds
$seconds = DurationUtil::toSeconds('2h 30m'); // 9000
$seconds = DurationUtil::toSeconds('2.5 hours'); // 9000
$seconds = DurationUtil::toSeconds('90 minutes'); // 5400
$seconds = DurationUtil::toSeconds('02:30'); // 9000
$seconds = DurationUtil::toSeconds('02:30:45'); // 9045
// Format seconds to human-readable strings
$duration = DurationUtil::toString(9000); // "2h 30m"
$duration = DurationUtil::toString(9000, false); // "2 hours 30 minutes"
$duration = DurationUtil::toString(9000, true, ['h', 'm']); // "2h 30m"
// Format as HH:MM
$time = DurationUtil::toHhMm(9000); // "02:30"
// Convert between DateInterval and seconds
$interval = new DateInterval('PT2H30M');
$seconds = DurationUtil::intervalToSeconds($interval); // 9000
$interval = DurationUtil::secondsToInterval(9000); // DateInterval object
use Kachnitel\DurationBundle\Form\Type\DurationType;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('duration', DurationType::class, [
'label' => 'Task Duration',
'help' => 'Examples: 2h 30m, 90 minutes, 02:30',
'
class Task
{
#[ORM\Column(type: 'integer', nullable: true)]
private ?int $duration = null;
public function getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration): self
{
$this->duration = $duration;
return $this;
}
}
use Kachnitel\DurationBundle\Service\DurationUtil;
// Only hours and minutes
$duration = DurationUtil::toString(93784, true, ['h', 'm']); // "26h 3m"
// Days, hours, and minutes
$duration = DurationUtil::toString(93784, true, ['d', 'h', 'm']); // "1d 2h 3m"
// All units
$duration = DurationUtil::toString(93784, true, ['y', 'mo', 'w', 'd', 'h', 'm', 's']);
// Output depends on the value
// Entity
class TimeEntry
{
#[ORM\Column(type: 'integer')]
private int $duration = 0;
}
// Form
$builder->add('duration', DurationType::class, [
'label' => 'Duration',
'help' => 'Enter duration (e.g., 2h 30m)',
]);
// Template
<div class="time-entry">
<strong>Duration:</strong> {{ entry.duration|duration }}
</div>
use Kachnitel\DurationBundle\Service\DurationUtil;
class ReportGenerator
{
public function generateTimeReport(array $tasks): array
{
$totalSeconds = array_sum(array_column($tasks, 'duration'));
return [
'total_time' => DurationUtil::toString($totalSeconds, false),
'total_hours' => DurationUtil::toHhMm($totalSeconds),
'task_count' => count($tasks),
'average_time' => DurationUtil::toString($totalSeconds / count($tasks)),
];
}
}