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 variable 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);
Related
i am storing date and time in database using php using gmdate() function in format "Y-m-d H:i:s". for e.g.
2014-03-10 12:35:55
Now,on getting this data into a php variable,for e.g.
$temp=2014-03-10 12:35:55
can i extract and display only the DATE portion excluding the TIME portion??
I am new to date and time manipulation.Any help?
you can get directly the date from database like this
select DATE(column_datetime) as date from yourtable
As Pramod answered,
date('Y-m-d', strtotime($temp));
works.
date('Y-m-d', strtotime($temp));
The above statement returns the date from datetime format
The following code example is adapted by this thread: How to convert date to timestamp in PHP?
There you can also read why it is not safe to rely on strtotime (answer by daremon)
$temp = '2014-03-10 12:35:55';
$a = strptime($temp, '%Y-%m-%d');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
Edit
From the PHP manual
Note: This function is not implemented on Windows platforms.
Try this:
Echo date('Y-M-D', $var);
So im trying to insert a time using an input text field into a database table with data type TIME.
The format of time that I want to insert should be like this:
H:MM pm// example: 6:30 pm
The problem is the am/pm doesnt insert in my database table.
Only the hour and minute.
Please give me idea how to solve this.
Better with sample codes. Thanks.
Data Type TIME is for storing time data type - that means no AM/PM. Store the data in Your database in 24 hour format and format it to 12 hour format with am/pm in PHP or MySQL using one of these:
PHP:
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');
or:
$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:
SELECT DATE_FORMAT('%h:%i:%s %p', time) FROM table;
Store the TIME as a standard format (18:30:00), and the format it however you want when you display it (Using DateTime objects or the date functions).
MySQL doesn't support extra formats when storing time data.
I think you want to add the jquery time picker value in your database with actual format in the database.
Here I have written some function
function update_time($time){
$ap = $time[5].$time[6];
$ttt = explode(":", $time);
$th = $ttt['0'];
$tm = $ttt['1'];
if($ap=='pm' || $ap=='PM'){
$th+=12;
if($th==24){
$th = 12;
}
}
if($ap=='am' || $ap=='AM'){
if($th==12){
$th = '00';
}
}
$newtime = $th.":".$tm[0].$tm[1];
return $newtime;
}
$time = update_time($_POST['time']); //here I am calling the function now you can insert the value in db
you just have to call the function and insert the returned value in database.
And while printing that you can do something like that echo date("h:i A",strtotime($time));
Change the type of the field to a varchar. TIME cannot store it like that. However, keep in mind that storing it like you want to will make it more difficult to provide localized results if that is something you will eventually need. That is, timezone support becomes difficult if you are not storing the timestamp itself, but rather a user-friendly representation.
EDIT: Or, DATETIME works as well, as was pointed out in the comments above.
You can use the DateTime Object in PHP which has functions to create a time object from any format and also has a function to output a time in any format like so
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
http://php.net/manual/en/datetime.createfromformat.php
You would be best changing the field type to 'VARCHAR (32)', and then writing the time with PHP.
Example: date('m/d/y g:i:sa');
Why do you want to store the am or pm anyhow? If you store the date/time as a unix epoch timestamp, you can format the date however you want in the program - not the database.
Example: time(); - Store this in an INT(8) field.
date('m/d/y g:i:sa, $time()); - Output from DB like this.
try .ToShortTimeString() after your date variable.
Hi i'm inserting a date entry into a field that has a Type Timestamp and field default CURRENT_TIMESTAMP ...my insert value will look like this
'.($data[16] == '' ? CURRENT_DATE() : $data[16]).'
how would I format the CURRENT_DATE() to be 30-Nov-10 to match what $data[16] format would be if not present. Also should I change the Field Type to Date and not Timestamp?
date($format,strtotime(CURRENT_DATE)) works for me.
CURRENT_DATE() is a MySQL function. You could use it as a string literal:
$sql_date = ($date == '') ?
"DATE_FORMAT(CURRENT_DATE(),'%d-%b-%y')" : "'$date'";
mysql_query("UPDATE foo SET date=$sql_date");
This assumes that $date is safe for SQL insertion.
Or you could just use PHP:
if (!$date) $date = date('%d-%M-%y');
mysql_query("UPDATE foo SET date='$date'");
Personally, I would generally use the PHP solution as it makes it easier to use the same query with the same safe parameter placeholders (not used in this example) regardless of how you build the date.
Also should I change the Field Type to Date and not Timestamp
If the time is irrelevant, then it should be a date field. However, if it's a date/timestamp field (and not a text field), you should be inserting in the YYYY-MM-DD format, and the above code is useless.
That is, if the field is text, then the above code could be useful. However, if you are storing simply a date into a single field, you should use a date field, In that case, the format you insert should always be in YYYY-MM-DD. When you retrieve the data, you can format it in the way you want for display.
Well without any further information
all i can tell you is
you should look into the function date and it's second arg
where you put a timestamp which you could create with mktime alt. strtotime
This question doesn't make sense.. I'm assuming you want to output the current date.. in which case you can change the format with the following:
$today = date("M/d/y");
also, time() will output the current date to the second in a unix timestamp
$today = time();
Using php I am inserting or updating the mysql database with create date or modified date using the variables
$datestring = "%Y:%m:%d %h:%i:%s";
$time = time();
$createdate= mdate($datestring, $time);
In this $createdate will be the variable I use to insert or update the table. But it's updating the wrong value. It's not the server time or localtime. Mostly it's 30 mins delay with the server's time.
Use date() function of PHP
$createdate= date('Y-m-d H:i:s');
Edit: after some googling it looks like you're using CodeIgniter. You should have mentioned that in your question.
The format string you're using doesn't match MySQL's date format. You want to use:
$datestring = '%Y-%m-%d %H:%i:%s';
use in mysql query like DATE_FORMAT(purchaseDate, "%Y:%m:%d %h:%i:%s") function
I want to allow my users to search the database for a row that was submitted on a certain day. The date is entered into the field in the database using the date() function which is great, but when i use the php strtotime function, of course the dates are not exactly the same.
Is there some clever mysql function that can help me with this?
I had an issue with this before.
You're best to generate a start and end date then use the BETWEEN function instead.
So something like this:
$start_date = "2009-01-01 00:00:00";
$end_date = "2009-01-01 23:59:59";
$sql = "SELECT * FROM table WHERE date BETWEEN '$start_date' AND '$end_date' AND id = 'x';
Of course you would just pull your dates from the DB using strtotime and append the time stamps - depends how you used date()
Hope this helps :)
You can use MySQL's DATE() function to extract date part of the timestamp:
select ... from table ... where DATE(date_column) = '2010-01-25';
If you have problem submitting '2010-01-25' from PHP, you can use PHP's date function with 'Y-m-d' as parameter to only get the date part.
$d = date('Y-m-d', strtotime(...));
Looking at your question closely, it seems you'll need both of those. First use PHP's date function to get only the date part and then use MySQL's date to match only those records.
PHP:
$timestamp_from_php = strtotime('December 25, 2009');
SQL:
select
`fields`
from
Table t
where
date_format('Y-m-d', t.`datetime_field`) = date_format('Y-m-d', '$timestamp_from_php')