0

Hey everyone,

I have the following varaibles:

$_POST
$_POST
$_POST

I need them to be put into a string format such as '2012-02-15' (Year-Month-Day).

Could someone please help me do this in php?

I've tried a few times but can't connect them with the "-" successfully.

Thanks for your help.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by bbman
0
<?php
if(isset($_POST['month']) && isset($_POST['day']) && isset($_POST['year']) && is_int($_POST['month']) && is_int($_POST['day']) && is_int($_POST['year']) && $_POST['month'] > 0 && $_POST['month'] < 13 && $_POST['day'] > 0 && $_POST['day'] < 32 && $_POST['year'] > 0 && $_POST['year'] < 2020){
  if(checkdate($_POST['month'],$_POST['day'],$_POST['year'])){
    $date = "{$_POST['year']}-{$_POST['month']}-{$_POST['day']}";
  }else{
    echo "This date does not exist";
  }
}else{
  echo "Date data not valid";
} 


?>

That's just a little clunky - and you could create nicer tests, e.g. stipulating better limits for the year. There are loads of ways to do it. You could use mktime() and date(), or even use the DateTime object.

Edited by diafol: n/a

0

$date = "{$_POST}-{$_POST}-{$_POST}";

Thank you so much. This was the line I needed. I wrote similar lines several times but they just didnt work.

I appreciate your help, keep up the good work :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.