Guide · 2026-11-21

How to Create a Gantt Chart in PHP: A Step-by-Step Guide

Building a project timeline in PHP can feel harder than it should. You need to show tasks, dates, and overlaps clearly, yet a basic list of deadlines hides how work connects. Add changing schedules or unclear ownership, and a timeline can quickly become difficult to maintain.

But here's the truth: you can create a useful Gantt chart in PHP without a specialized charting package. In this guide, I’ll walk you through preparing task data, calculating durations, and drawing a simple chart with SVG. You’ll also learn when a PHP-generated chart makes sense, how to handle common problems, and when a project management platform can help you coordinate the work behind the timeline.

How to Create a Gantt Chart in PHP

The simplest approach is to store each task’s name, start date, and end date in a PHP array, then use PHP to calculate positions and render SVG bars. SVG keeps the chart crisp at different sizes and works directly in a web page.

Here’s the process. You can adapt the dates, task names, and styling to match your project.

  1. Prepare your task records. Give every task a label, a start date, and an end date. Use the Y-m-d date format so PHP can parse dates consistently.
  2. Set the chart’s timeline. Choose the earliest start and latest end dates. These define the date range represented by the chart.
  3. Choose chart dimensions. Set a label area, the width of the timeline, and the height of each task row.
  4. Calculate each task’s position and length. Measure how many days separate the task’s start from the chart start. Use its duration to determine the bar width.
  5. Render the SVG. Draw a label and horizontal bar for each task. Escape task names before placing them in the SVG.
  6. Test dates and presentation. Check overlapping tasks, long labels, and tasks that begin or end on the chart’s boundaries.

Build a task array

Start with a few sample tasks. The end date is treated as inclusive in this example, so a task beginning and ending on the same date appears as a one-day bar.

<?php
$tasks = [
    [
        'name' => 'Plan requirements',
        'start' => '2025-04-01',
        'end' => '2025-04-04',
    ],
    [
        'name' => 'Create interface',
        'start' => '2025-04-03',
        'end' => '2025-04-10',
    ],
    [
        'name' => 'Test and refine',
        'start' => '2025-04-09',
        'end' => '2025-04-14',
    ],
];
?>

The overlap is intentional: interface work begins before requirements planning ends, while testing starts before interface work finishes. A Gantt chart makes those overlaps visible in a way a simple task list does not.

Calculate dates and positions

Next, find the overall timeline and calculate the start offset and duration for every task. Using DateTimeImmutable keeps the date calculations clear and avoids accidentally changing a date object during a calculation.

<?php
$dates = [];

foreach ($tasks as $task) {
    $dates[] = new DateTimeImmutable($task['start']);
    $dates[] = new DateTimeImmutable($task['end']);
}

$chartStart = min($dates);
$chartEnd = max($dates);

$totalDays = (int) $chartStart->diff($chartEnd)->days + 1;

$labelWidth = 170;
$dayWidth = 34;
$rowHeight = 42;
$topPadding = 24;
$chartWidth = $labelWidth + ($totalDays * $dayWidth);
$chartHeight = $topPadding + (count($tasks) * $rowHeight);

foreach ($tasks as $index => $task) {
    $taskStart = new DateTimeImmutable($task['start']);
    $taskEnd = new DateTimeImmutable($task['end']);

    $offsetDays = (int) $chartStart->diff($taskStart)->days;
    $durationDays = (int) $taskStart->diff($taskEnd)->days + 1;

    $x = $labelWidth + ($offsetDays * $dayWidth);
    $y = $topPadding + ($index * $rowHeight);
    $barWidth = $durationDays * $dayWidth;
}

The + 1 includes both the first and last dates. Without it, a task covering April 1 through April 4 would appear to span only three days.

Render task bars with SVG

Use the calculated values to draw each row. This example includes vertical day markers and task labels. The label is escaped with htmlspecialchars() before it becomes SVG text.

<?php
echo '<svg xmlns="http://www.w3.org/2000/svg" ';
echo 'width="' . $chartWidth . '" height="' . $chartHeight . '" ';
echo 'viewBox="0 0 ' . $chartWidth . ' ' . $chartHeight . '" ';
echo 'role="img" aria-label="Project Gantt chart">';

for ($day = 0; $day < $totalDays; $day++) {
    $x = $labelWidth + ($day * $dayWidth);
    $date = $chartStart->modify('+' . $day . ' days);
    $dateLabel = $date->format('M j');

    echo '<line x1="' . $x . '" y1="0" ';
    echo 'x2="' . $x . '" y2="' . $chartHeight . '" ';
    echo 'stroke="#e5e7eb" />';

    echo '<text x="' . ($x + 4) . '" y="16" ';
    echo 'font-size="11" fill="#475569">';
    echo htmlspecialchars($dateLabel, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    echo '</text>';
}

foreach ($tasks as $index => $task) {
    $taskStart = new DateTimeImmutable($task['start']);
    $taskEnd = new DateTimeImmutable($task['end']);

    $offsetDays = (int) $chartStart->diff($taskStart)->days;
    $durationDays = (int) $taskStart->diff($taskEnd)->days + 1;

    $x = $labelWidth + ($offsetDays * $dayWidth);
    $y = $topPadding + ($index * $rowHeight);
    $barWidth = $durationDays * $dayWidth;
    $label = htmlspecialchars(
        $task['name'],
        ENT_QUOTES | ENT_SUBSTITUTE,
        'UTF-8'
    );

    echo '<text x="8" y="' . ($y + 19) . '" ';
    echo 'font-size="13" fill="#1f2937">' . $label . '</text>';

    echo '<rect x="' . $x . '" y="' . $y . '" ';
    echo 'width="' . $barWidth . '" height="24" rx="4" ';
    echo 'fill="#2563eb" />';
}

echo '</svg>';
?>

Place the PHP inside a page that outputs HTML, then open that page in a browser. You should see three labeled rows, with bars positioned according to their dates. For a longer project, consider showing weekly markers rather than a label for every day.

Choose the Right Approach for Your PHP Gantt Chart

Hand-rendered SVG works well when you need a compact visual in an existing PHP application. You control the output and avoid adding a charting dependency. For example, a small internal page can display a release schedule using a task array and the SVG code above.

However, drawing the chart yourself means you also own interactive behavior. If people need to drag bars, zoom into dates, or update task details directly, a JavaScript Gantt library may be a better fit. PHP can still prepare the task information and deliver it to the browser.

Here's why: chart rendering and project coordination are different jobs. PHP can render a timeline, but a chart alone does not assign ownership, track approvals, or keep decisions connected to tasks. Pick the approach that solves your actual workflow, not just the visual requirement.

Improve Accuracy, Readability, and Maintenance

A chart is only helpful when its dates and labels are understandable. Choose a consistent date format, decide whether end dates are inclusive, and show the timeline scale clearly. A four-day task should not look like a three-day task because the calculation omitted the final day.

Keep the visual readable as the project grows. If 50 daily columns crowd the page, switch to weekly or monthly markers. If task names are long, increase the label area or provide a wrapping approach rather than allowing text to run beneath the bars.

For example, a two-week sprint may benefit from daily markers, while a six-month rollout is easier to scan with weekly or monthly divisions. The right scale helps someone spot a deadline without inspecting every date label.

Validate task dates

Check that each start date is valid and that the end date does not come before it. If task details come from a form or another system, validate them before rendering. Otherwise, an invalid date may cause a misleading bar or a runtime warning.

Consider time zones and working days

The sample treats every calendar date as a day. That is suitable for broad planning, but it does not account for weekends, holidays, or working hours. If a task should last five working days, add business-day calculations instead of using the simple calendar-day difference.

Time zones matter when tasks include times rather than dates. Store and compare timestamps consistently, then display them in the project’s chosen time zone. For date-only plans, using a consistent date format can keep the calculations simpler.

When a Custom Chart Is Not Enough

A custom PHP chart gives you control over how a timeline looks. It does not automatically provide a complete project workflow. As task counts increase, you may need dependencies, permissions, change history, sprint planning, or reporting alongside the visualization.

Consider a product launch with design, engineering, testing, and approval tasks. A PHP page can show when each task is scheduled, but a team still needs to know who owns each item and what is blocking progress. Those needs call for project management practices or a platform that supports them.

You might be wondering: should you build more features into the PHP application? Add custom behavior when it is central to your product. If the team mainly needs planning, tracking, and shared knowledge, managing that work in a project platform may be simpler than maintaining a growing collection of custom features.

Project Coordination Solution: ONES.com

ONES.com product screenshot

Value Proposition

ONES.com is a unified platform for project management and knowledge management, powered by ONES Assistant. ONES Project handles project management, while ONES Wiki provides knowledge management; the products are sold separately.

If your PHP Gantt chart displays dates but your team needs a fuller workflow, ONES Project can support planning and tracking. It is a Jira alternative for teams that want project tools with self-hosted deployment options.

Core Capabilities

Application Scenarios

Software teams: A PHP-rendered timeline can provide a project-specific visual, while ONES Project supports sprint planning, custom workflows, and progress reporting around the work. This can help when a team needs more than a static schedule.

Organizations with restricted network requirements: Teams that cannot use a public cloud environment can consider On-Premise, Private Cloud, or Air-gapped deployment options. They can coordinate project activities in a deployment that fits their environment.

Teams connecting project work with shared knowledge: ONES Project and ONES Wiki address project management and knowledge management as separate products. A team can choose the product or products that match its needs rather than assuming they are bundled together.

ONES.com offers a free plan for up to 30 seats. If you are evaluating it, compare the workflow and deployment requirements with your team’s actual needs; a custom PHP chart may still be useful for a specific presentation or application screen.

Common Challenges When Building Gantt Charts in PHP

Bars appear one day too short

Problem: The duration calculation counts the gap between dates but excludes the end date. Solution: Add one day when calculating an inclusive calendar-day duration, as the example does with + 1.

Tasks extend beyond the chart range

Problem: A task starts before the displayed range or ends after it, leaving part of the bar outside the intended chart area. Solution: Set the chart range deliberately, then clamp bar positions and widths to its boundaries when displaying only a selected period.

Long schedules become crowded

Problem: Daily date labels overlap on a multi-month project. Solution: Increase the timeline scale to weeks or months, or display fewer labels while retaining subtle grid lines.

Task labels create unsafe markup

Problem: Unescaped task names may interfere with SVG markup or create a security risk. Solution: Escape dynamic text with htmlspecialchars() before placing it inside SVG or HTML.

Calendar days do not match work duration

Problem: A task appears to span weekends even though the team plans in working days. Solution: Calculate business days and account for holidays when the schedule depends on working time.

FAQs

Can I create a Gantt chart in PHP without a charting library?

Yes. You can calculate task positions in PHP and render the chart as SVG, as shown in this guide. That approach works for a basic timeline with task labels and bars. If you need features such as dragging, zooming, or interactive updates, a JavaScript Gantt library may be more suitable for the browser-side display.

Should PHP or JavaScript render the Gantt chart?

Use PHP when you want to generate a straightforward chart on the server or embed a timeline in an existing PHP page. Use JavaScript when people need to interact with bars, change dates visually, or zoom without reloading the page. You can also combine both: PHP prepares task information, and JavaScript handles interactive rendering.

How do I show task dependencies in a PHP Gantt chart?

Store dependency relationships alongside task details, such as an identifier for the task that must finish first. Then draw connectors between related bars in SVG or use a charting library with dependency support. Be clear about whether a dependency means one task must finish before another starts, since that affects schedule calculations.

How can I skip weekends when calculating task duration?

The example counts calendar days, so weekends are included. For working-day schedules, iterate through dates and count only the weekdays your team uses, while excluding configured holidays. For complex calendars, use a date library or a tested business-day function so different projects follow consistent rules.

Is SVG a good format for a PHP Gantt chart?

SVG is a practical choice for a chart made of labels, lines, and bars. It scales cleanly across screen sizes and can be generated directly as markup. If the chart grows complex or needs interactive controls, consider using a JavaScript charting tool instead of building every behavior yourself.

Conclusion

You can create a Gantt chart in PHP by representing tasks with start and end dates, calculating their positions, and drawing bars with SVG. Define whether dates are inclusive, validate task ranges, escape labels, and choose a timeline scale that makes the schedule easy to read.

A custom chart can solve a focused display need, but it may not cover the coordination around the work. When you need workflows, sprint planning, reporting, or deployment choices, assess whether a project platform such as ONES Project fits alongside your PHP application.

If a timeline feels confusing today, start with a few real tasks and make their overlaps visible. Then refine the chart or workflow based on how your team actually plans and tracks progress.