I am writing a PHP form for my website. The HTML side asks the user for a date which they enter in MM/DD/YYYY format. When that string is sent to PHP the following code changes it to the form that MySQL will recognize
$date = $_POST['date'];
$sqldate = date('Y-m-d', strtotime($date));
However when that date is entered into my MySQL database it is entered as 1970-01-01 and I can't figure out why.
NOTE: If I echo $sqldate I get the error Use of undefined constant sqldate - assumed 'sqldate' in C:/MYDIRECTORY
How about this?
//build a date
$date = date_parse_from_format("m/d/Y", $_POST["date"]);
//output the bits
$sqldate = "$date[year]-$date[month]-$date[day]";
A Unix timestamp is the number of seconds since 1970-01-01 so your call to strtotime() is returning 0.
How is DateTime working for you?
<?php
$dateStr = '08/18/2014';//$_POST['date'];
$dateTime = new DateTime($dateStr);
echo $dateTime->format('Y-m-d');
You're writing that the date format of the form is MM/DD/YYYY. Do you validate the input values to be sure that the format is always given?
THis worked for me
$input_date= trim($_POST["input_date"]);
$strtotime= strtotime($input_date);
$date_format= date('Y-m-d',$strtotime);
Related
This might be very simple, but I still need help with this.
I have this portion of php code:
$END = NULL;
if (isset ($_POST['end'])){
$END = date_format($_POST['end'], "Y-m-d H:i");
}
Where $_POST['end'] is a date and time that I get in format dd-mm-YYYY HH:mm. The problem is, as you can guess, that it doesn't transform my input to the Y-m-d H:i format, it just doesn't do anything. But I've followed what I've seen in another code that does indeed work. What am I doing wrong here?
Ignore the fact that I don't check if the input is well written, I assume that it will be.
This is because date_format accepts an object and not a string. You should use the function date and pass to it's second argument a timestamp.
Use date_create() function to convert string to date and then pass it to date_format() function.
if (isset ($_POST['end'])){
$date = create_date( $_POST['end'] );
$END = date_format($date, "Y-m-d H:i");
}
I'm comparing a date with current date(i.e. today's date). It is expected that the error should come only when the date to be compared is greater than today's date. It should not come for date which is less than or equal to today's date.
I've written following code for it.
$submission_date = $_POST['submission_date']; //The date in mm-dd-yyyy format that is to be tested against today's date. The value in $submission date is 12-25-2014
//This is a future date. Today's date is 12-10-2014 in dd-mm-yyyy format
$current_date = date('m-d-Y');
if (strtotime($submission_date) > strtotime($current_date))
{
echo "Future date not accepted";
}
With the above code I'm not getting errors for future dates, sometimes I'm getting error for previous dates as well.
How to optimize and make this code correct and standard?
If posted format is in m-d-Y, then you cannot convert it to unix timestamp directly with strtotime() function, because it will return false.
If you need to use strtotime() then change the input format to m/d/Y by simple str_replace().
On the other hand, you could use DateTime class, where you can directly compare objects:
$submission_date = DateTime::createFromFormat('!m-d-Y', $submission_date);
$today_date = new DateTime('today');
if ($submission_date > $today_date) {
echo "submission_date is in the future\n";
}
demo
If you need to extract some information from DateTime objects, use format() method on them, which accepts same format as date() function:
echo $today_date->format('m/d/Y'); # 12/11/2014
echo $today_date->format('m-d-Y'); # 12-11-2014
echo $today_date->format('Y-m-d'); # 2014-12-11
echo $today_date->format('Y-Y-Y'); # 2014-2014-2014
demo
I think you need to compare date in 'Y-m-d' or 'd-m-Y' format. I think it is not possible to compare date in 'm-d-Y' format
As we have made changes in your code and we test it works from my side so can you try below.
$submission_date = $_POST['submission_date'];
$current_date = date('d-m-y H:i:s');
if (strtotime($submission_date) > strtotime($current_date))
{
echo "Future date not accepted";
}
Hope this helps.
In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .
$start_date = date('Y-m-d', $_POST['start_date']);
the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.
However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...
Just do this:
$start_date = date('Y-m-d', strtotime($_POST['start_date']));
You could also use strtotime() on your $_POST.
$start_date = date('Y-m-d', strtotime('05/12/2014'));
try to use
$date = str_replace('/', '-', $_POST['start_date']);
$start_date = date('Y-m-d', strtotime($date));
For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )
Is there a way to convert an input time string (ex: 01:13) to a Zend date object, so that I store it later in a timestamp column in a Mysql database.
Examples:
If the current datetime is 2013-07-15 17:33:07 and the user inputs 18:05 the output should be 2013-07-15 18:05:00.
If the current datetime is 2013-07-15 17:33:07 and the user inputs 02:09 the output should be 2013-07-16 02:09:00. Notice that since the time entered was lower than the current time, so it was treated as tomorrows time.
I simply want to get the next point in time that satisfies the entered time. I'm open for solution using plain PHP or Zend_Date.
I think you should compare the current time with the time entered by the user and create a DateTime object of either "today" or "tomorrow". DateTime accepts strtotime() relative time parameters.
Quick hack. Works as of today, 15.07.2013 23:58 local time:
$nextTime = new DateTime('today 18:10');
if ($nextTime < new DateTime('now')) { // DateTime comparison works since 5.2.2
$nextTime = new DateTime('tomorrow 18:10');
}
echo $nextTime->format('d.m.Y H:i:s');
here is working example for you just add your dynamic variable to check date with user inputs
You can use mktime function to manage your date.
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d"),date("Y")));
echo "current time".$current_time = date('Y-m-d H:m:s');
echo "<br>User input is ".$input_date;
if(strtotime($current_time) > strtotime($input_date)){
$input_date = date("Y-m-d H:i:s",mktime(18,05,0,date("m"),date("d")+1,date("Y")));
echo "in";
}else{
// nothing to do
}
echo "<br> result->".$input_date;
i hope it will sure solve your issue
I have get data from oracle database and date value kept on 27-MAY-09. I need to insert this value to mysql database via PHP. I need to convert date format as 2009-05-27.
Any one know about it please let me know correct php statement for do this.
use date() function
$date = '27-MAY-09';
$newData = date('Y-m-d', strtotime($date));
php fiddle
$date = DateTime::createFromFormat('j-M-y', $inputDate);
$newDate = $date->format('Y-m-d');
PHP 5.3 not earlier.
try this
$date1 = "27-MAY-09";
$data2 = date("Y-m-d",strtotime($date1));