date format in MySQL table - php

I have the following two names in MySQL table:
message is type text
date is type datetime
<?php
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><div><a><? echo $row['message'] ?></a></div></td>
<td><div><a><? echo $row['date']; ?></a></div></td>
</tr>
<?php
}
?>
The output of the above table shows date in this format 2012-08-05 17:43:57
How to print date in this format 2012-08-05 (without time)?
I cannot change type datetime in MySQl (it’s required in different pages in different formats).
I’ve tried the following, but it doesn’t help (no printing at all)
<td><div><a><? echo $rows['date'](“Y-m-d”); ?></a></div></td>
p.s. other combinations of date format give me syntax error.
Any suggestion would be much appreciated,

You want
<?php echo date('Y-m-d', strtotime($row['date'])); ?>
strtotime converts your datestring into a unix-timestamp
then, the date function formats it properly according to the string.

You want the date() to change the format.

There is something called TO_CHAR method in MS-SQL and Oracle in which you can pass the format you want your date in, so while fetching the data you can change it

You should use format function
echo new DateTime($row['date'])->format('Y-m-d');
UPDATED thanks to #Erty

http://php.net/manual/en/datetime.format.php
<td><div><a><? echo new DateTime($row['date'])->format('Y-m-d'); ; ?></a></div></td>

every one else is suggesting you do it with php, i always do this with mySQL's own date function:
SELECT ... DATE_FORMAT(Date_Field ,'%Y-%e-%c') ...
DATE_FORMAT

You first need to convert it to a date:
$tmpdate = strtotime($row['date']);
$date = date('Y-M-d', $tmpdate);
echo $date;
Of course you may short it down, as others suggested, but i prefer "the long way" to improve readability and help you get up to speed faster when you go back and review old code in the future. :-)

Related

PHP / SQL display dates in format

In an SQL database I'm storing various dates, such as date of birth and date they joined my system, in the standard SQL format YYYY-mm-dd, however I wish to display these to my British users (all my users) in the format dd-mm-YYYY.
I've tried pretty much everything I found online about doing this, however cannot decipher how it's done correctly. The code I list below is what I am currently using, however it does not display the correct date stored in the database and instead uses a completely random date of 01-01-1970. Some assistance on resolving this issue would be greatly appreciated.
while($row = $results->fetch_assoc()){
array_push($_SESSION["ActQueue"], array($row["username"], $row["surname"], $row["forename"], date('d-m-Y', $row["dob"]), $row["gender"], $row["joined"]));
}
$data = 0;
echo json_encode(['Username'=>$_SESSION["ActQueue"][0][0], 'Surname'=>$_SESSION["ActQueue"][0][1],'Forename'=>$_SESSION["ActQueue"][0][2],'DoB'=>$_SESSION["ActQueue"][0][3], 'Gender'=>$_SESSION["ActQueue"][0][4], 'Joined'=>$_SESSION["ActQueue"][0][5]]);
You need to convert your plain text date to time before passing to date() function
date('d-m-Y', strtotime($row["dob"]))
The date you receive 01-01-1970 its not a random date but its actually the first date from unix system
You need to use :
date('d-m-Y', strtotime($row["dob"]))
strtotime
You can use this this code to format your date
(new \DateTime($row["dob"]))->format('d-m-Y');

PHP date_format TIME not diplayed correctly using Joomla

I'm having some problems displaying the TIME Only (H:i) in a row.
<td headers="qf_start_date" nowrap="nowrap"><?php echo date_format($course->start_date, '%H:%i'); ?></td>
This is the code, and as you can see, i want to see, under the start_date table in phpmyadmin, only Hours and minutes, nothing else. In the start_date query I want to have only, at the same time Y-m-d but i want to see in a page, only the hours and minutes...
I tried using date_format but it doesn't work...
What can i do?
I tried to include JDate but i think that i used a wrong sintax so i see blank page...
Thanks
To be more clear, visit: http://new.ivao.ch/index.php?option=com_seminarman&view=category&cid=4:rfe-geneva-departure&Itemid=227 and the table that i'm working in STD. It's blank...dunno why..
Thanks in advance
please followw at below :
<?php
$date = "6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP", $date));
?>
or
<?php
$date = date_create('2000-01-01 12:15:25');
echo date_format($date, 'H:i:s');
?>
or
<?php
$valid_date = date( 'm/d/y g:i A', strtotime($date));
?>
or
<td headers="qf_start_date" nowrap="nowrap">
<?php
$valid_date = date( '%H:%i', strtotime($course->start_date));
echo $valid_date;
?>
Since it looks like you're working with Joomla, you can use the JDate class to help you.
If your $course->start_date is a valid date string (can be converted with strtotime, you may do the following):
// May fix class JDate not found errors. At the top of your script.
jimport( 'joomla.utilities.date' );
// ...
$date = new JDate($course->start_date)
echo $date->format('H:i');
Alternatively, (from the comments) it seems like your date string is not formatted as a UNIX timestamp. If you're not using Joomla, or do not want to use JDate you may use:
echo date('H:i', strtotime($course->start_date));
What's happening here is, for most date processing functions in PHP, you must pass it a valid UNIX timestamp.
A UNIX timestamp is measurement of the number of seconds since the Unix Epoch on January 1st, 1970 at UTC.
You can convert most date strings into one (including mysql DATETIME columns by passing it into php's strtotime() function.
From there, you pass it into date (or your desired date formatting function) with a desired format. Hope this helps!

convert one date format to another

I need help to convert a formatted date format as stated below:
How can I convert 2010-02-06 14:44:43 to dd/mm/yyyy
Thanks
Great place to start for this type of general information, php docs, specifically related to your date format question: http://us3.php.net/manual/en/function.date.php
-- Edit --
Answer with substr() forced me to do the work... using substr() to format a date is not the best option as PHP has built-in functions for that.
var_dump(date('d/m/Y', strtotime('2010-02-06 14:44:43')));
$date = new DateTime('2010-02-06 14:44:43');
echo $date->format('d/m/Y');
Output:-
06/02/2010
See the manual
Try this:
<?php
$time = strtotime('2010-02-06 14:44:43');
echo date('d/m/Y',$time);
?>

Converting MySQL datetime to RSS pubDate using PHP?

i was wondering how would I convert datetime using PHP to work with RSS feeds pubDate?
Why is my browser 3 hours off when displaying the time? Is there a way to correct this?
Assuming $row is the resultant row of mysql_fetch_assoc() of your result, and your date column is called date:
<pubDate><?php echo gmdate(DATE_RSS, strtotime($row['date'])); ?></pubDate>
You can use the DateTime class as follows:
$objDate = new DateTime($itemDate);
$rssDate = $objDate->format(DateTime::RSS);
Where $row['date'] is the date from your row in MySQL. $rssDate with contain the properly formatted RSS Date.
<pubDate><? echo date("r", strtotime($data["added_on"])); ?></pubDate>
Courtesy of the first google result on 'mysql rss pubdate'
Edit: The off-time is probably due to timezone issues; it's not your browser, probably, it's the time being generated by PHP. Have you configured your PHP instance properly?
Edit: Sorry for the confusion guys, forgot to added the "Edit:" remark. No need to bash on the asker!
<?php
$query = mysql_query("SELECT added_on FROM 'table');
while($row = mysql_fetch_array($query)){
?>
<pubDate><? echo date("r", strtotime($data["added_on"]));} ?></pubDate>
As BoltClock used the gmdate you WILL get the time in GMT, so you could calculate where you are easier.
That said PHP is server side thus your browser might be +- 3 hours because the server is in a +-3 time zone from your browser.
If you are experiencing that problem locally check for weird locales.
Use strtotime() to convert it into Unix timestamp then convert it into desired format using date()

How do I convert a mySQL DATETIME type to a string for use in Javascript?

I want to display a DATETIME I get from a mySQL database as a String in Javascript.
I'm using PHP to put the DATETIME into a variable: $mydatetime.
It displays fine on the PHP page, but not in my javascript function.
<?php
echo $mydatetime --> 2010-04-19 13:00:00
echo "<script language=javascript>myfunction($mydatetime);</script>";
?>
Javascript
function myfunction(mydatetime) {
alert(mydatetime);
}
This produces an error in my console: Uncaught SyntaxError: Unexpected number
I've tried many things to try to convert mydatetime to a string, but nothing seems to be working.
Any help is appreciated, thanks.
I don't know what format you need for your function - you don't specify.
Chances are you just need to enclose the value in quotes:
echo "<script language=javascript>myfunction('$mydatetime');</script>";
But the general way to convert dates from mySQL to something else in PHP is:
Turn the date into a timestamp: $mytimestamp = strtotime($mydatetime); Strtotime can read a great number of time formats, DATETIME is among them. Manual on strtotime here
Output the timestamp in any format you like: echo date("Y-m-d", $mytimestamp); Manual on date formatting options here
It's a string, so Javascript needs it in quotes:
<?php
echo $mydatetime --> 2010-04-19 13:00:00
echo "<script language=javascript>myfunction('$mydatetime');</script>";
?>

Categories