My select statement in PHP is
"select * from table";
I use the following PHP statement to display date & time of MySQL field.
<?php
echo $row['mdate'];
?>
The result come like this
2010-03-09 16:59:18
I want to view the result in the following format
09-03-2010 16:59:18
and I want to view the result in the following format
09-03-2010 4:59:18 PM
without defining any extra function. I can only modify my echo statement.
<?php echo $row['msgdate']; ?>
or
I can also modify my select statement.
See date_format():
select *, date_format(mdate, '%d-%m-%Y %H:%i:%s') AS formated_date from tabl;
And use formated_date in jouw php-code.
You can do the formatting directly in the database as Frank Heikens shows in his answer, or you can do it in PHP. Convert the mySQL date value to a UNIX timestamp using
$timestamp = strtotime($row["mdate"]);
then you can use all options of date() to format it, for example:
echo date("Y-m-d H:i:s", $timestamp); // returns 09-03-2010 16:59:18
both approaches are equally valid; I personally like to modify the value in PHP.
Related
I have an html input field with a php echo in it. The echo reads from a database field with a timestamp. I need to change the format of the outputted date.
The database has the default 'datetime' sql field and format.
How can i change this to dd/mm/yyyy hh:mm?
I have tried reading the docs on changing a date via php but because of the variable and database field, i dn't seem to be able to formulate it correctly.
Here is an example of a field i'm trying to change:-
<?php echo $update->micp_edit_TIMESTAMP; ?>
You can select the value from database with proper format if you want. Let's try like this way using DATE_FORMAT()
SELECT DATE_FORMAT(your_column_name, '%d/%m/%Y %H:%i') FROM your_tablename
Quick and dirty
//asumming this is a DATETIME field in your database
echo date('d/m/Y H:i', strtotime($update->micp_edit_TIMESTAMP));
If you use MySQL use #Curious_Mind 's answer
As per php manual:
Object oriented style
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
Procedural style
<?php
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
?>
The above examples will output:
2009-02-15
See: http://php.net/manual/en/datetime.createfromformat.php
You can adapt the examples to use the formats you have and require by looking in the manual which explains the format strings. And of course substitute the variables by your own variables.
i have used Now() and it stores something like "2017-01-10 19:28:58" in database which is the current time of user's device.
But i want it like January 10 at 7:28pm . how to do it in simple way. please help
You can use PHP date function with its formatting options
<?php
echo date("F d \a\t g:ia");
If you want more detailed formatting please visit the PHP manual here. You can find everything you need with detailed examples.
you could use the PHP date function
date(F d \a\t\ g:ia);
Your post is ambiguous, but it sounds like you are referring to the SQL NOW() function. The format of the data stored could be a DATETIME or TIMESTAMP or a combination of DATE and TIME columns. The format you get when you retrieve this value from the database depends on that data storage format, your query, and how the value is dealt with when it gets into PHP.
If you want to reformat it using SQL, consider a function like DATE_FORMAT. Assuming your column with the date is called my_column, here's a sample query.
SELECT DATE_FORMAT(my_column, '%M %e at %l:%i%p');
EDIT: you can also use DATE_FORMAT on the result of the NOW() function:
SELECT DATE_FORMAT(NOW(), '%M %e at %l:%i%p');
You might have to tweak the second parameter to get the date format you want.
If your date is stored as a string (VARCHAR or whatever) in your database, then you would need to convert it to a timestamp or datetime first and then use the PHP date function to output the variant you want. Assuming $row is a record from your data table:
$date_string = $row["my_column"];
$stamp = strtotime($date_string); // NOTE that this will assume some timezone
if (!$stamp) {
die("Could not create a timestamp from the date");
}
echo date("F j \a\t g:ia);
You could also use PHP'S DateTime functions which are more modern, if somewhat verbose in usage.
I have a calendar in html form and I want to insert this date into MySQL. The default MySQL date is 0000-00-00. But in my country the format is DD/MM/YYYY. So what to do to fix it. Thank you. I am using PHP.
You must use one format in your HTML page, and another format in your database.
So, if you want to store a date like this '12/05/2008' into mySql, you must transform it like this:
$date = '12/05/2008';
$dateToStore = date('Y-m-d', strtotime(str_replace('/','-',$date)));
And if you wonder why, you need to replace the '/' with '-' to make php know that the first part of the data string is the day, and then the month (as I think is your case).
MySQL the date format is always YYYY-MM-DD. To convert it to another format, you need to manually convert the retrieved date to the desired format like
$displayDate=date("d/M/Y", strtotime($mysqldate));
Method 1
You cant insert into DD/MM/YYYY format. Instead while rendering it in view file you can change into desired format.
<?php
$date = $result['db_date']; // I ASSUMED YOUR DB FIELD IS db_date
$desiredFormat = date('d/m/Y', strtotime($date)); // CONVERTING INTO YOUR FORMAT
echo '<pre>'; print_r($desiredFormat); // DISPLAYING IT
?>
Method 2
You can retrieve from database in your desired format using below
SELECT *, DATE_FORMAT(YOUR_DATE_FIELD, "%m/%d/%Y") AS date FROM YOUR_TABLE;
Use MySQL STR_TO_DATE
Try this mysql query :-
INSERT INTO `table`(`date`) VALUES (STR_TO_DATE('10/10/2015', '%d/%m/%Y'))
How would i go about checking to see if an auction has expired in my database? I have a datetime column in MySQL that i believe is of the following format: YYY-MM-DD hh:mm:ss. If this is the case would the following check work - i.e. want to select only expired auctions from the table in the database...
<?php
//Some code
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ($date_time > finish_time)
");
?>
While "finish time" is a column in the database of the above cited format. Presuming this works how actually do i get the current date into the same format? If anybody knows i would very grateful cheers. Even more so if the above query wouldn't work and something else is required. Thanks again.
Oh and of course i would define the date_time variable to start with
Do you actually need the $date_time variable? The easiest way to do this would be SELECT * FROM auction WHERE finish_time < NOW(). That way you'll get your results and don't have to set the date from PHP.
You can use
<?php
//Some code
$date_time=strtotime($date_time);
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ( $date_time > UNIX_TIMESTAMP(finish_time))
");
?>
This solution:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
is from this post: PHP Zend date format
and here's another solution: how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript?
and to do it in php:
How to get the current date and time in PHP?
The function date is made for it:
// Get the current date
$date_time = date("y-m-d H:i:s");
Look at the documentation page to see other format flags ;)
i have column named postDate defined as timestamp.
when i print it directly:
echo $result['postDate'];
i do get that what is stored(eg. 2011-03-16 16:48:24)
on the other hand when i print it through date function:
echo date('F/j/Y',$result['postDate'])
i get December/31/1969
what am i doing wrong?
many thanks
try this.
date('F/j/Y',strtotime($result['postDate']));
as timestamp is required, not formatted date as second parameter.
or you can also try
SELECT UNIX_TIMESTAMP(postDate) as postDateInt from myTable
instead of SELECT postDate from myTable
and then have this in your code.
date('F/j/Y',$result['postDateInt']);
The PHP date function looks for an int time() as the 2nd param. Try using strtotime()
echo date('F/j/Y', strtotime($result['postDate']) );
Why not format the date as needed in your MySQL query?
SELECT DATE_FORMAT(postDate, '%M/%D/%Y') as date from table
The PHP `date()' function expects a number for the second parameter - ie a unix timestamp.
You can convert a SQL date string (or virtually any other date string) into a timestamp in PHP by using the strtotime() function. At least two other answers have already suggested this.
However, I would suggest that you'd be better off getting the date out of your database in unix timestamp format in the first place. You can do this by querying using the MySQL UNIX_TIMESTAMP() function, as follows:
SELECT UNIX_TIMESTAMP(mydatefield) AS mydatefield_timestamp FROM mytable
..obviously, replacing the field and table names as appropriate.
Then you will get the date in timestamp format in your returned dataset in PHP, which you can pass directly into the date() function as follows:
echo date('F/j/Y',$result['mydatefield_timestamp']);
Hope that helps.