How can substr and time conversion be effectively used in PHP for data manipulation?

To manipulate data in PHP using substr and time conversion, you can extract a portion of a string using substr and convert it to a timestamp using functions like strtotime or date_create. This can be useful for tasks like extracting a specific part of a date-time string or converting a date-time string to a timestamp for calculations.

// Example of using substr and time conversion in PHP
$dateString = "2022-10-15 08:30:00";
$timeString = substr($dateString, 11); // Extracts the time portion (08:30:00) from the date-time string

$timestamp = strtotime($dateString); // Converts the date-time string to a Unix timestamp
$dateObj = date_create($dateString); // Creates a DateTime object from the date-time string

echo "Time: " . $timeString . "\n";
echo "Timestamp: " . $timestamp . "\n";
echo "Date Object: " . date_format($dateObj, 'Y-m-d H:i:s') . "\n";