I'm using 2 variables: check-in, check-out
Their format is: dd/mm/yyyy
I want separate the day, month and year into single variables, in order to compare the integers of:
dd(check-in) with dd(check-out),
mm(check-in) with mm(check-out), and
aaaa(check-in) with aaaa(check-out),
so that the check-in cannot be done after the check-out, and an error appears when somebody try.
On the php file:
$in_date = $_POST['check-in'];
settype( $in_date, "string");
$in_yyyy = substr( $in_date , 6, 4);
settype( $in_yyyy, "integer");
$in_mm = substr( $in_date , 3, 2);
settype( $in_mm, "integer");
$in_dd = substr( $in_date , 0, 2);
settype( $in_dd, "integer");
$out_date = $_POST['check-out'];
settype( $out_date, "string");
$out_yyyy = substr( $out_date , 6, 4);
settype( $out_yyyy, "integer");
$out_mm = substr( $out_date , 3, 2);
settype( $out_mm, "integer");
$out_dd = substr( $out_date , 0, 2);
settype( $out_dd, "integer");
Suppose I typed in input:
check-in: 16/12/2016
check-out: 23/01/2017
However, those are the values the variables takes on:
$in_date = 16/12/2016
$in_yyyy = 2017 // THAT'S WRONG
$in_mm = 01 // THAT'S WRONG
$in_dd =23 // THAT'S WRONG
$out_date = 23/01/2017
$out_yyyy = 2017
$out_mm = 01
$out_dd =23
The problem seems to be caused by "substr". It seems to take on, no matter what, always the last value attributed to it.
Any suggestion?
I would strongly recommend that you utilize it with Datetime object when you are working with date/time although
explode()
orsubstr()
will also work.Benefit of it is that also you can use this datetime objects to compare your check-in and check-out way easier, or you can even easily calculate nights etc. Try following code: