# Date and Time Formatting Guide

## Overview
The application now uses date and time formats from the Settings page. When you change the date or time format in Admin Settings, it will automatically apply throughout the application.

## Helper Functions

Three helper functions are available globally:

### 1. `format_date($date, $format = null)`
Formats a date string using the format from settings.

```php
// Uses date_format from settings (e.g., 'm/d/Y')
echo format_date('2026-03-26'); // Output: 03/26/2026

// Override with custom format
echo format_date('2026-03-26', 'd-M-Y'); // Output: 26-Mar-2026
```

### 2. `format_time($time, $format = null)`
Formats a time string using the format from settings.

```php
// Uses time_format from settings (e.g., 'h:i A')
echo format_time('14:30:00'); // Output: 02:30 PM

// Override with custom format
echo format_time('14:30:00', 'H:i'); // Output: 14:30
```

### 3. `format_datetime($datetime, $dateFormat = null, $timeFormat = null)`
Formats a datetime string using both date and time formats from settings.

```php
// Uses both formats from settings
echo format_datetime('2026-03-26 14:30:00'); // Output: 03/26/2026 02:30 PM

// Override with custom formats
echo format_datetime('2026-03-26 14:30:00', 'Y-m-d', 'H:i'); // Output: 2026-03-26 14:30
```

## Usage Examples

### In Table Displays
```php
<td><?= e(format_date($task['scheduled_date'])) ?></td>
<td><?= e(format_time($task['scheduled_time'])) ?></td>
```

### In Combined Date/Time Display
```php
<td><?= e(format_date($r['scheduled_date'])) ?> <?= e(format_time($r['scheduled_time'])) ?></td>
```

### In Text Output
```php
echo "Task scheduled for " . format_date($date) . " at " . format_time($time);
```

## Available Date Formats in Settings

- `Y-m-d` → 2026-03-26 (YYYY-MM-DD)
- `m/d/Y` → 03/26/2026 (MM/DD/YYYY)
- `d/m/Y` → 26/03/2026 (DD/MM/YYYY)
- `d-M-Y` → 26-Mar-2026 (DD-Mon-YYYY)

## Available Time Formats in Settings

- `H:i` → 14:30 (24-hour format)
- `h:i A` → 02:30 PM (12-hour format with AM/PM)

## How to Update Existing Code

### Before:
```php
<td><?= e((string)$r['scheduled_date']) ?> <?= e((string)$r['scheduled_time']) ?></td>
```

### After:
```php
<td><?= e(format_date($r['scheduled_date'])) ?> <?= e(format_time($r['scheduled_time'])) ?></td>
```

## Pages Already Updated

- `public/admin/index.php` - Admin overview (time display)

## Pages That Need Updating

To apply date formatting throughout the application, update these files:

1. `public/admin/task_assignment.php` - Task lists and calendar
2. `public/admin/task_add.php` - Task creation page
3. `public/staff/index.php` - Staff task list
4. `public/staff/history.php` - Staff task history
5. `public/customer/index.php` - Customer requests
6. `public/customer/my_services.php` - Customer services
7. Any other pages displaying dates/times

## Testing

1. Go to **Admin Panel → Settings**
2. Change **Date Format** to `m/d/Y` (MM/DD/YYYY)
3. Change **Time Format** to `h:i A` (12-hour)
4. Click **Save Settings**
5. Visit any page with dates/times
6. Dates should now display as 03/26/2026
7. Times should now display as 02:30 PM

## Important Notes

- The functions handle empty dates gracefully (return empty string)
- Invalid dates are returned as-is without error
- Settings are cached for performance
- Default formats are used if settings are not available
