Reputation: 97
I have a function GetAsNumber(date) in filemaker.
Ex: GetAsNumber(10/10/2008) returns 733325.
I need to write function GetAsNumber with PHP.But with function mktime() in php i can't do it the same filemaker.
Upvotes: 1
Views: 142
Reputation: 53536
This function seems to be replicating the same thing as the FileMaker's GetAsNumber
function :
function GetAsNumber($str) {
static $DATE_OFFSET = 719163; // adjust because PHP is from 1980 and Filemaker from 0001
static $SECONDS_IN_DAYS = 86400; // get only days from date
if (preg_match('/^(\\d{2})[-\\/\\.](\\d{2})[-\\/\\.](\\d{2}|\\d{4})$/', trim($str), $matches)) {
$val = intval(strtotime("{$matches[1]}-{$matches[2]}-{$matches[3]}") / $SECONDS_IN_DAYS) + $DATE_OFFSET;
} else {
$val = floatval(preg_replace('/[^\\d\\.]/', '', $str));
}
return $val;
}
Tested with these values :
echo GetAsNumber("FY98") . ' == 98' . PHP_EOL;
echo GetAsNumber("$1,254.50") . ' == 1254.5' . PHP_EOL;
echo GetAsNumber("2 + 2") . ' == 22' . PHP_EOL;
echo GetAsNumber("TKV35FRG6HH84") . ' == 35684' . PHP_EOL;
echo GetAsNumber("10/10/2014") . ' == 735516' . PHP_EOL;
echo GetAsNumber("10/10/2008") . ' == 733325' . PHP_EOL;
Upvotes: 1
Reputation: 388
Odd function, but this appears to do the trick:
function getasnumber($date)
{
$dt_date = new DateTime($date);
$dt_start = new DateTime('1/0/0001');
return $dt_date->diff($dt_start)->days;
}
var_dump(getasnumber('10/10/2008')); // 733325
var_dump(getasnumber('10/10/2014')); // 735516
The return values appear to be right according to the first date you supplied, and this other documentation.
Upvotes: 1