Format Date Number from Database
Not sure if I should edit this question or just ask another question in a different post. I am having a hard time doing the reverse of what I originally asked. How do I get the "date number" in php. I want to assign the current date number to a vari开发者_Python百科able to so I can use it in my query to compare what is already in the database.
I can not see the SQL Server database but I am connecting to it using ODBC and PHP on a Linux box. I was told that the field "meeting_start" was a "Date Number". When I get the value of the "meeting_start" the format looks like this.
2010-08-24 20:00:00.000
I need both the date and time but for different parts of my function. Is there a way to extract the data for the date and save to a variable and then extract the data for the time and save to a different variable. Below is me trying to take a stab at it.
$time_date_data = "2010-08-24 20:00:00.000" $meeting_date = get_date($time_date_data); $meeting_time = get_time($time_date_data);
You're looking for the date
function:
$meeting_date = date('Y-m-d', strtotime($time_date_data));
$meeting_time = date('H:i', strtotime($time_date_data));
For $time_date_data = '2010-08-24 20:00:00.000'
, you'll get:
$meeting_date = '2010-08-24';
$meeting_time = '20:00';
You can change the format by changing the first argument to date()
according to the docs...
Two options here. You can do it either on the SQL Server side or the application side.
For SQL: If you want the date only, in format YYYY/MM/DD, do this:
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]
If you want the time only, in format HH:MM:SS, do this:
SELECT CONVERT(VARCHAR(8), GETDATE(), 108)
Alternatively, you can do this all in PHP by using the "date" function, something like:
$theDate = date("YYYY/MM/DD", strtotime($theFullDate))
$theTime = date("HH:MM:SS", strtotime($theFullDate))
Try:
$stamp=strtotime("2010-08-24 20:00:00.000");
$date=date("Y-m-d",$stamp);
$time=date("H:i:s",$stamp);
or:
list($date,$time)=explode(" ","2010-08-24 20:00:00.000");
$meeting_date = substr($time_date_data, 0, -13);
$meeting_time = substr($time_date_data,11);
精彩评论