Trimming or converting a mysql date field with php - php

I am displaying the contents of a datetime field in mysql in a table with php, and want to show just the date. Is there a way to convert or do this, or must the actual string be editied or trimmed?

Using strtotime...
A simple way is like this:
$date="2009-01-27 14:30:22";
echo strftime("%d %m %Y", strtotime($date));
i.e. turn your mysql date into a time value with strtotime, then reformat it with strftime.
However this only works for dates which can be represented by a unix timestamp - so no dates before 1st Jan 1970 or after 2038
Using PEAR's Date class...
The PEAR Date classes for a more thorough approach which should cope better with historical dates:
$date=new void Date("2009-01-27 14:30:22");
echo $date->format("%d %m %Y");
...or use your DB
Alternatively, have your db format it for you with DATE_FORMAT
SELECT col1,col2,DATE_FORMAT(col2, '%W %M %Y') as col2readable FROM mytable;

You could do it with PHP, but if you don't need the additional baggage, you could consider doing it in your query and return only what you need. This example uses DATE_FORMAT():
"SELECT col1, col2, DATE_FORMAT(dateCol, '%d-%m-%Y') as 'newDate'
FROM tablename"

Related

How to change Day-D-Month-Y - Time Date Format in SQL/PHP

I'm working with a date string in this format Wednesday 21 February 2018 - 09:35.
I'm just curious, is there an easy way to change this into a UNIX datetime format in either SQl or PHP or would I have to write a unique case for each one.
I have tried changing this with the PHP function strtotime but I can't get it to work with this specific format.
Thanks!
You can actually handle this directly in MySQL, using the STR_TO_DATE and UNIX_TIMESTAMP functions:
SELECT UNIX_TIMESTAMP(
STR_TO_DATE('Wednesday 21 February 2018 - 09:35', '%W %e %M %Y - %H:%i')) AS ts
FROM dual;
Demo
The basic idea is to first convert your text timestamp into a formal date using STR_TO_DATE, then convert that date into a UNIX timestamp using UNIX_TIMESTAMP.

Mysql compare date time

I have varchar field in database and format of that field is like '10 Feb 2016 08:26 PM'.
Now I want to fetch upcoming data, So how can i get that ?
$ctime=strtotime(date("Y-m-d H:i:s"));
if($type=='1'){
$books = $objData->getAll("select *
from bookings
where custID='".$custID."'
and fromTime>'".$ctime."'");
}
But I am not getting correct result, please help.
First of all, as mentioned in comments you should use proper types for dates. To answer your question, it is still possible to achieve, using STR_TO_DATE mysql function.
$objData->getAll("select * from bookings where custID=".$custID."'
AND unix_timestamp(STR_TO_DATE(fromTime, '%d %b %Y %h:%i %p')) > ".time());
Link: Convert VARCHAR timestamp to TIMESTAMP?

Changing date from query with PHP

I have a query (written to be easier from a class)
$cms->my_query('SELECT * FROM location');
Which will return an array
Though I have a DATE type in the mySQL Table which it is formatted like so 2014-06-22
Is there a way I can format so it's like this Nov 04 2008 11:45 PM with using DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p") now I believe DATE cannot use this properly so i'd have to use DATETIME but if that is the case it's fine but how do I select all and change date at the same time?
Example
$cms->my_query('SELECT * FROM location DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p")');
I just don't want that ugly 2014-06-22 and I have very little knowledge of mySQL and I am learning as I try new things out. So if someone who is more skilled please explain the best scenario for me, I'd like to learn and I am willing!
The first argument of DATE_FORMAT() is the date you want to format. Putting NOW() in there means you will return the current date.
First, you'll need to change the date column to DATETIME, then use that column as the first argument to DATE_FORMAT. Try this:
SELECT *, DATE_FORMAT(mydate ,"%b %d %Y %h:%i %p") as date_added FROM location
Where mydate is the DATETIME column from the table.
See demo
The column need to be in type DATETIME. With date_time_column is a column in location table. Should be like this:
$cms->my_query('SELECT DATE_FORMAT(date_time_column,"%m-%d-%Y %r") FROM location');

Fixing old MySQL date field mistake

I've have a date field in my table that stores dates from a form in a Weekday, Day Month, Year string.
Example: Wednesday, 02, November 2010
the field is also a varchar.
can I loop through the entire table with a php script to convert the dates to a mysql date format or perform this with an SQL query?
I'm not sure that I can preform some certain statistical reports that involve picking out certain dates and date ranges in the format I have now. What are my options?
No need to get PHP involved. It can be done directly in MySQL:
ALTER TABLE yourtable ADD fixeddate date;
UPDATE yourtable SET fixeddate=STR_TO_DATE(bad_date_field, '%W, %d, %M %Y');
relevant docs here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
PHP has a built in class called DateTime. Here is a snippet of code that should get you started.
$date = DateTime::createFromFormat('l, d, F Y', 'Wednesday, 02, November 2010');
echo $date->format('Y-m-d');
You can find more info about this class at http://au.php.net/manual/en/book.datetime.php

Returning rows on a certain date in MySQL

I've looked through past questions - but couldn't find a good match. How do I return all MySQL rows where the column timestamp is on day X, where day X is contained in a PHP variable the following format: "13 Apr 2011", and the timestamp is in database is in the following format: "2011-02-25 22:00:17"?
SELECT * WHERE timestamp (need to add here)
Thanks!
If the date string (in your PHP variable) has a consistent format, you can use the MySQL function str_to_date to convert it into a DATETIME suitable for comparison.
SELECT * FROM tablename WHERE DATE_FORMAT(timestamp,'%e %b %Y') = 'date coming from php'
(It's the opposite of andri's suggestion, it converts the timestamp into a string for comparision)
What you need is STR_TO_DATE
SELECT * WHERE DATE(`timestamp`) = STR_TO_DATE('$var','%M %d %Y')
More here

Categories