Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combing numbers but not adding them in PHP

I'm trying to do a server-side validation of the date chosen by user and the current date and here's what I have:

$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$dateval = $year + $month + $day;

if ($dateval - $today < 0) {
$datepassed = 'no';
}
else {
$datepassed = 'yes';    
}

Now as far as I know, everything is working flawlessly except the fact that the $dateval variable just adds up all the numbers instead of putting them together to form the date chosen by the user. for example 20110719 returns 2037. How can I make a veriable that combines the numbers without adding them? Any help is appreciated.

like image 450
Claudio Delgado Avatar asked Jun 26 '26 06:06

Claudio Delgado


2 Answers

if you want to concatenate them as strings, use the dot (.)

$dateval = $year . $month . $day;

however, since you are working with dates, consider using the mktime function

$dateval = mktime(0, 0, 0, $month, $day, $year)
like image 166
Al W Avatar answered Jun 28 '26 19:06

Al W


If you're concatenating strings, you should be using the concatenation operator, .:

$dateval = $year . $month . $day;

Otherwise, PHP will be clever and convert your strings to integers, a consequence of weak typing as implemented by the PHP language.

like image 38
You Avatar answered Jun 28 '26 21:06

You



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!