Oihana PHP

makeTimestampedDirectory.php

Table of Contents

Functions

makeTimestampedDirectory()  : string|null
Creates a directory named with a formatted timestamp.

Functions

makeTimestampedDirectory()

Creates a directory named with a formatted timestamp.

makeTimestampedDirectory([string|null $date = null ][, string $basePath = Char::EMPTY ][, string $prefix = Char::EMPTY ][, string $suffix = Char::EMPTY ][, string|null $timezone = 'Europe/Paris' ][, string|null $format = 'Y-m-dTH:i:s' ]) : string|null

Combines a date/time string (or the current time) with optional prefix, suffix, and base path to generate a unique directory name. The directory is created if it does not already exist.

Parameters
$date : string|null = null

Optional date/time string to use. If null or invalid, the current date/time is used ("now").

$basePath : string = Char::EMPTY

Optional base path in which to create the directory. Defaults to an empty string.

$prefix : string = Char::EMPTY

Optional string to prepend to the directory name.

$suffix : string = Char::EMPTY

Optional string to append to the directory name.

$timezone : string|null = 'Europe/Paris'

Timezone identifier (e.g., 'Europe/Paris'). Defaults to 'Europe/Paris'.

$format : string|null = 'Y-m-dTH:i:s'

Date format compatible with DateTime::format(). Defaults to 'Y-m-d\TH:i:s'.

Tags
throws
DirectoryException

If directory creation fails due to an error.

example

Example 1: simple directory in current path, using current date‑time

use oihana\\files\\createTimestampedDirectory;
use oihana\\enums\\Char;

$dir = createTimestampedDirectory();
// e.g. ./2025-07-15T10:30:12

Example 2 : directory inside /tmp with custom prefix/suffix and explicit date

$dir = createTimestampedDirectory
(
    date:     '2025-12-01 14:00:00',
    basePath: '/tmp',
    prefix:   'backup_',
    suffix:   '_v1'
);
// e.g. /tmp/backup_2025-12-01T14:00:00_v1

Example 3 : use a different timezone and format

$dir = createTimestampedDirectory
(
    date:     null,              // now
    basePath: Char::EMPTY,       // current directory
    prefix:   'log_',
    suffix:   Char::EMPTY,
    timezone: 'UTC',
    format:   'Ymd_His'          // 20250715_083012
);
// e.g. ./log_20250715_083012
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string|null

The full path of the created directory, or null on failure.


        
On this page

Search results