I m setting up a script to run daily and check for members who meet a certain age - automated emails can be set up in a CMS and assigned to be sent at any age, either in months or years. To handle this via PHP and MySQL, the number of months is passed as a parameter to a method, which I deal with as below. However, I m not sure I m going about this in the easiest way! Partly because of the formatting of the UK date format, I m converting from string to datetime to unix timestamp to make the comparison. Can anyone find a better way of going about this? Thanks
// If num of months provided is a year, make calculation based on exact year
if ($age_in_months % 12 == 0)
{
// Using 365 days here (60 * 60 * 24 * 365 = 3153600)
$clause = WHERE ROUND((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(dob, "%d/%m/%Y"))) / 31536000) = . $age_in_months;
}
else
{
// Using 30 days as avg month length (60 * 60 * 24 = 86400) - convert months to days
$clause = WHERE ROUND((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(STR_TO_DATE(dob, "%d/%m/%Y"))) / 86400) = . $age_in_months * 30;
}