Trouble using MSSQL datetime in PHP (via SQLSRV) - php

I am going round in circles with this one! I'm doing the following:
Retrieving a date from an MSSQL datetime field via SQL/PHP
Sending the date to a new PHP page via the querystring
Trying to use that date in a new SQL query
I'm hitting problems here.
If I use echo:
echo $_REQUEST['rSessionDate'];
output: 15/10/2012
Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.
If I use date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
If I use strtotime():
echo strtotime($_REQUEST['rSessionDate']);
output: (nothing)
If I use date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
If I use date() with strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
I'm sure I'm totally missing something simple.
EDIT: I've tried a few new functions I found:
$rSessionDate = new DateTime($_REQUEST['rSessionDate']);
echo $rSessionDate->format('Y-m-d H:i:s');
output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
and:
$rSessionDate = date_create($_REQUEST['rSessionDate']);
echo date_format($rSessionDate, 'Y-m-d H:i:s');
output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
EDIT 2:
I have tried using CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"

These might help to shed some light on what you're looking for.
http://www.ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime() is returning an epoch timestamp in your example above.
or check CAST and CONVERT (refers to MSSQL2000 but may still help you)
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx

if the date was retrieved from an MSSQL table and you want to use strtotime() in PHP and also don't want to change the date format to yyyy-mm-dd then you can use
CONVERT(VARCHAR(30), DateFromMSSQL, 121) as DateFromMSSQL

Related

DateTime format expects parameter 1 to be string, Object given - Symfony2

I'm having some troubles using the format function in Symfony2 when trying to insert a date interval into a table, for the purpose of setting due dates for created invoices.
Here is what I have:
$today = new \DateTime();
$interval = $today->add(new \DateInterval('P1M'));
$invoice->setDueDate($interval->format('Y-m-d H:i:s'));
However, when I hover over the format parameter in PHPStorm, it tells me that it's expecting a DateTime object not a string, and I get the following error in my profiler:
Error: Call to a member function format() on string
So, I changed the line to this:
$invoice->setDueDate($interval->format(new \DateTime()));
But when I run that, my profiler gives this error:
Warning: DateTime::format() expects parameter 1 to be string, object given
It almost seems like a catch 22 situation! I am really baffled, do I use a string or a DateTime object, because either one fails yet warns me I need to use one or the other..
Any ideas?
I’d guess that $invoice->setDueDate() is the one expecting a DateTime instance. So the line should be $invoice->setDueDate($interval);

inject date string as srtotime

I am trying to resolve this since hours, and I tried different solutions offered here within the website, but for some reasons the website always returns an error when applying the suggestions.
I have a form field which uses a datepicker, and the output is a date in the format: November 6th, 2013
Now, the mysql database and the rest of the script only works with a ten digit date format, which I have been told is the unix timestamp format and that it can be converted using the strtotime. So I am trying to convert the string into strtotime, but cant resolve this. For now I have:
$insertData['enddate'] = $this->input->post('openDays');
which returns the date "November 6th, 2013" and I changed it to
$insertData['enddate'] = strtotime $this->input->post('openDays');
which returns the error: Parse error: syntax error, unexpected T_VARIABLE in /home/xxx/public_html/app/controllers/project.php on line 214
any suggestions on how to correctly apply this?
Thanks
Try this, you are doing a mistake by using strtotime.
$insertData['enddate'] = strtotime($this->input->post('openDays'));
Reference link: Strtotime PHP
strtotime is a function , so you need to enclose it like this (using parenthesis)
$insertData['enddate'] = strtotime($this->input->post('openDays'));
-------^ -------^
EDIT :
I suggest you use DateTime instead of strtotime
$dt = DateTime::createFromFormat('H:i', '17:00');
$insertData['enddate']=$dt->format('H:i');

Contruct a datetime with specific format

i have a date string from my db, the date is 16/11/2010 and its format is d/m/Y, i want to modify its like this.
<?php
$date_from_db= '16/11/2010'; // format is d/m/Y
$date = new DateTime($date_from_db);
$date-> modify('+1 week');
echo $date-> format('d/m/Y') ;
?>
i have got this error
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (16/11/2012) at position 0 (1).
How can i fix this?
try
$date = DateTime::CreateFromFormat("d-m-y", "16-11-2010");
Unfortunately that is not one of the time formats supported by DateTime class constructor.
This page shows valid formats
http://www.php.net/manual/en/datetime.formats.date.php
You should use an actual SQL-compliant date field (typically YYYY-MM-DD) in your DB. Or if you can't change the way your are storing the dates, use DateTime::CreateFromFormat as suggested in other answer.
I would highly suggest using a more standard storage format though.

PHP date: non well formed numeric value encountered

I'm trying to store a date from a PHP form into an MS SQL database and am really struggling. The date is in string format, and in 'DD/MM/YYYY' format so I've tried to use strtotime() to convert it, but I just get 1970-01-01 01:00:00 from that.
The code I've tried is the following:
$requested = $_REQUEST["TextboxRequiredBy"];
var_dump($requested);
echo "<br/>";
$daterequested = date("Y-m-d H:i:s", strtotime($requested));
echo $requested . " becomes " . $daterequested . "<br/>";
mssql_bind($stmt, "#Parameter", $daterequested, SQLFLT8, false);
What I get on screen is:
string(10) "31/07/2012"
31/07/2012 becomes 1970-01-01 01:00:00
Warning: mssql_bind(): Unable to set parameter in xxx.php on line 110
Warning: mssql_execute(): message: Procedure or function 'SPNAME' expects
parameter '#Parameter', which was not supplied. (severity 16) in xxx.php
I've searched around and I just can't seem to find a way to convert the string successfully. I've used the following on another page that runs a date comparison, but this brings back an error that date_format() expects parameter 1 to be DateTime, boolean given:
$datefrom = date_create($requested);
$daterequestedfrom = date_format($datefrom,'Y-m-d H:i:s');
Can anyone suggest how I can successfully convert the string into a date?
That date format is not a default format. You probably want to look at the date_create_from_format function. This will allow you to specify the format of the time string you are trying to input.
Link to PHP documentation
For some reason, PHP doesn't like the slashes in strtotime. Convert them to dots and the script should work.
$requested = str_replace('/', '.', $_REQUEST["TextboxRequiredBy"]);

PHP datetime add one second [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date time
Trying to add one second to a datetime that is input by the user
$values['start_date_'.$j.'-'.$i] is a valid datetime string, however the following code is throwing an error
$priceStart = date('Y-m-d H:i:s',strtotime($values['start_date_'.$j.'-'.$i]));
date_modify($priceStart, '+1 second');
$priceStart =date_format($priceStart, 'Y-m-d H:i:s');
The error is "date_modify() expects parameter 1 to be DateTime, string given in... on line..."
same error follows for date_format()
what is the correct syntax for this?
Use a DateTime object instead. It's much more powerful and easy for this one.
$priceStart = new DateTime("#" . strtotime($values['start_date_'.$j.'-'.$i]));
$priceStart->modify("+1 second"); //You're pretty much done here!
echo $priceStart->format("Y-m-d H:i:s"); //Just to see the result.
date() gives you a string. date_modify needs a DateTime object.
The easiest way to do what you want is simply adding one to the value returned by strtotime():
$priceStart = date('Y-m-d H:i:s',strtotime($values['start_date_'.$j.'-'.$i]) + 1);
Or, you can create a DateTime object:
$priceStart = new DateTime('#' . strtotime($values['start_date_'.$j.'-'.$i]));
and the rest of your code should start working.

Categories