Wordpress, php. Using substr() on more variables creates conflict

147 Views Asked by At

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?

2

There are 2 best solutions below

0
On

I would strongly recommend that you utilize it with Datetime object when you are working with date/time although explode() or substr() will also work.

$dt = DateTime::createFromFormat('d/m/Y', '16/12/2016');

echo "Year:  " . intval($dt->format('Y')) . "\n";
echo "Month: " . intval($dt->format('m')) . "\n";
echo "Day:   " . intval($dt->format('d')) . "\n";

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:

<?php

$dt = DateTime::createFromFormat('d/m/Y', '16/12/2016');

echo "Year:  " . intval($dt->format('Y')) . "\n";
echo "Month: " . intval($dt->format('m')) . "\n";
echo "Day:   " . intval($dt->format('d')) . "\n";

$dt_out = DateTime::createFromFormat('d/m/Y', '10/12/2016');

if ($dt > $dt_out) {
    echo "Check-in cannot be after check-out\n";
}

$dt_out = DateTime::createFromFormat('d/m/Y', '18/12/2016');

if ($dt < $dt_out) {
    echo "This is good!\n";
}

echo "Nights: " . $dt->diff($dt_out)->format("%a") . "\n";
0
On

Assuming you always get the date in the below format, you can use the explode() function in PHP to separate the day, month and year from the date.

$date  = '16/12/2016';
$parts = explode('/', '', $date);
$day   = $parts[0];
$month = $parts[1];
$year  = $parts[2];
unset($parts);        // clean up

Your code may look something similar to:

$dates = array(
    'check_in' => $_POST['check_in'],
    'check_out' => $_POST['check_out']
);

foreach($dates as $_date)
{
    settype( $_date, "string");
    $parts = explode('/', '', $_date);
    $day   = $parts[0];
    $month = $parts[1];
    $year  = $parts[2];
    settype( $day, "integer");
    settype( $month, "integer");
    settype( $year, "integer");
}

unset($_date);       // clean up