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()
Related
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');
In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.
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. :-)
I am trying to display a time I have in my database. I managed to have it display a time in the correct format for what I need, but for some reason, it is only displaying '4:00' every time.
Here is my code:
date('g:i', strtotime($row['startTime']))
An example of I have the time displayed in my database is like this: 00:12:30
Why is it showing '4:00' every time?
strtotime expects a datetime format ... you should do
date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
Whats the underlying database, and what datatype does the startTime column have? Peering at the closest php code I have, strtoime works fine with a DATETIME representation in the DB (MySQL).
strtotime converts a date time string to a Unix timestamp.
Perhaps your $row['startTime'] doesn't qualify as a date time string.
None of the examples here discussed a date time string which did not include a date.
The link also said that if strtotime is confused, it returns random results. I would add a few more format characters and see what else is returned.
As noted the problem is the use of strtotime(). The following works on my machine, if it's of any use:
$date_text = $row['startTime']; // assuming the format "00:12:30"
list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
/* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
*/
echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";
What is the simplest, fastest way to complete the PHP code below such that the output is in a user-friendly format (for example, "October 27, 2006")?
$result = mysql_query("SELECT my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = ???($row['my_timestamp']);
echo $formatted_date;
You could use MySQL to do this for you,
$result = mysql_query("SELECT DATE_FORMAT(my_timestamp, '%M %d, %Y) AS my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = $row['my_timestamp'];
echo $formatted_date;
Or use PHP,
$result = mysql_query("SELECT my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = strftime('%B %d, %y', $row['my_timestamp']);
echo $formatted_date;
I tend to do the date formatting in SQL, like Aron's answer. Although for PHP dates, I prefer using the DateTime object (PHP5+) over date:
$timestamp = new DateTime($row['my_timestamp']);
echo $timestamp->format('F j, Y') . '<br />';
echo $timestamp->format('F j, Y g:ia');
You want the date() function.
If you've got a DATE or DATETIME column, you can use SELECT UNIX_TIMESTAMP(mycolumn) AS mycolumn to convert it to a unix timestamp for the date function to use.
You should definitely use the DateTime class (or any home grew equivalent class), over de Date function, and not use timestamps:
Timestamp don't work well in all environments for dates before 1970 - if you deal with birthdays, you're relying on code that may break on some servers
Be also very carefull of the use of strftime, it looks like a nice function, but it is very unrelyable, as it depends on setlocale, which is process-wide. What this means is that if your server is a windows box, you have one process per processor, and then the rest is multi-threaded - in other words, one setlocale in one script will affect the other scripts on the same processor - very nasty !
At the end of the day, don't rely on timestamps, unless you are in an english only environment, and deal with dates between 1970 and 2032 only...!
If you have your tables indexed on that date field and you use a mysql data format function in your call.. (ie .. SELECT DATE_FORMAT(my_timestamp, '%M %d, %Y) AS my_time ) it will destroy your indexing. Something to keep in mind. We have seen dramatic increases in speed in removing all functioning from our sql statements and letting php handle it all.
Functioning such as formatting dates and simple math
I use:
date("F j, Y", strtotime($row['my_timestamp']))
or you can change the SELECT to: DATE_FORMAT(field,'%d %M, %Y') as datetime
You have a choice. You can use the date() function in PHP and process the output from MySQL, or you can use the date_format() function in MySQL and have it return a formatted string to begin with.
In my experience is that it doesn't really matter which you use, but BE CONSISTENT. They have different formatting parameters, so if you use both in a given application you'll spend a lot of time trying to remember if 'W' gives you the name of the day of the week or the week of the year.
Have the database do the formatting for you. It is far less susceptible to error because you are not reading a string and converting it. Instead, the database is going from its native format to a string.