How can one convert a date string into a UNIX timestamp for age calculation in PHP?
To convert a date string into a UNIX timestamp for age calculation in PHP, you can use the strtotime() function to convert the date string into a UNIX timestamp. Once you have the UNIX timestamp, you can then calculate the age by subtracting the timestamp from the current time and converting the result into years.
$dateString = '1990-05-15'; // Date string to convert
$timestamp = strtotime($dateString); // Convert date string to UNIX timestamp
$age = floor((time() - $timestamp) / 31556926); // Calculate age in years
echo $age; // Output the age
Related Questions
- How can the socket_select function be utilized in PHP for better error handling?
- What are common reasons for the error "could not determine the SMTP to connect" when using PHP scripts for email sending?
- What are the best practices for handling form submissions in PHP to ensure data is properly inserted into a database?