mysql - php match values with a user defined formula stored in a table and perform the requested mathematical operation -
i have user defined formula stored in database table string "({arrival_date}-{departure_date})". have below rows selected table.
departure_date arrival_date
2015-01-01 2015-03-01
2015-02-01 2015-02-10
i want use formula ({arrival_date}-{departure_date}) , difference in days i.e (2015-03-01 - 2015-01-01) = 2 etc rows. suggestions on how can match value formula?
thank in-advance.
once have stored 2 dates in 2 variables, can try this:
<?php $str1 = '2015-01-01'; $str2 = '2015-03-01'; $datetime1 = new datetime($str1); $datetime2 = new datetime($str2 ); $interval = $datetime1->diff($datetime2); echo $interval->format('%r%a days'); ?>
btw don't know format datetime accepts (yyyy-mm-dd or yyyy-dd-mm) or if depends on server's international options.. if doesn't work can use datetime::createfromformat
manually specify fields.
edit:
i don't know curly braces templates, if want result difference, can convert datetime
object in timestamp representation:
$datetime1 = new datetime($str1); $datetimets1 = $datetime1->gettimestamp(); // or, alternatively, can use $datetimets1 = $datetime1->format("u");
the 2 options should same dates after january 1, 1970.
the 2 variables $datetimets1
, $datetimets2
2 integers. difference difference between 2 dates expressed in seconds (since there 86400 seconds in day, have divide result 86400). can write
$datetimets1 = $datetime1->gettimestamp() / 86400; $datetimets2 = $datetime2->gettimestamp() / 86400;
because 2 dates not have "half days".
Comments
Post a Comment