In PHP, how do you convert a mysql timestamp like this "1125443836"
to an xml/date-time like this:
<wp:post_date>2011-01-25 02:10:32</wp:post_date>
UPDATE:
The columns in the mySQL are stored as int(10).
Based on samples below, this is what I tried with two sample values in my database.
Something is wrong, maybe hash or Unix dates stored in mySQL table?
$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));
$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',strtotime($testSqlDateStamp));
echo "<BR>";
Results:
DateTest=1969-12-31 18:00:00
DateTest=1969-12-31 18:00:00
Second Update: worked without calling strtotime
$testSqlDateStamp = "1125444107";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);
$testSqlDateStamp = "1125443836";
echo "<BR>DateTest=".date('Y-m-d G:i:s',$testSqlDateStamp);
Results:
DateTest=2005-08-30 18:21:47
DateTest=2005-08-30 18:17:16
'Y-m-d G:i:s' is not it. At least not in 2015 (I realize this is old).
In php 5.2.0 the DateTime class has a constant for this:
echo 'XML time: ' . date( DateTime::RFC3339, time() ) . '<br>';
DateTime::RFC3339 is set to "Y-m-d\TH:i:sP".
date("Y-d-m G-i-s",$time);
That should do it.
http://php.net/manual/en/function.date.php
EDIT: this won't work for mySQL timestamps, they must be converted to unix timestamps via strtotime() info: PHP: strtotime - Manual.
<?php echo '<wp:post_date>' . date('Y-m-d G:i:s',strtotime($yourMysqlDateStampVar)) . '</wp:post_date>';
Related
I echo $istoriko['timestamp'] from mySQL and I get 2014-04-24 00:10:29
How can I change it to
24/04/14 (or 2014) 00:10
Thank you
Try this, its simple
You can set any date format now in date functions
echo date("d/m/y",strtotime($istoriko['timestamp']));
Follow functions can help you:
$date = '2014-04-24 00:10:29';
$date = date('d/m/Y H:i',strtotime($date));
echo $date
I have something in my database that shows the time when the users who signed up on my recorded .. When the person makes the user saves the script (time ();) currently in the database under the variable name "REGTIM".
If I use echo to print it out, I get for example:
1375202508
How can I make this a date if it is possible? or for example as mentioned below.
for example: 08/06/2013 2:11
You can use date() function with timestamp mention in the second argument,
date ( string $format [, int $timestamp = time() ] )
or in your case:
date('m/d/Y H:m', 1375202508);
you can read more on http://php.net/manual/en/function.date.php
You could have got the answer to that with a little bit of searching. But here you go:
echo date('m/d/Y h:m', 1375202508); // 07/30/2013 10:07 -- 12-hour format
echo date('m/d/Y H:m', 1375202508); // 07/30/2013 22:07 -- 24-hour format
See the documentation for more options: http://php.net/manual/en/function.date.php
that's probably just a unix timestamp, so use FROM_UNIXTIME() and DATE_FORMAT() to do the conversion/formatting inside your query, or use date() in PHP to work with the timestamp directly.
e.g.
echo date('d/m/Y h:m', $timestamp)
or
SELECT DATE_FORMAT('%d/%m/%y %h:%m', FROM_UNIXTIME(REGTIM))
When using the following
echo date('D',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D',strtotime("2013-06-16T18:00:00-07:00"));
First it returns Sun and the Second returns Mon. I'm not really sure why or how to correct! The Date:"2013-06-16T06:00:00-07:00" is data I'm retrieving from a XML file. The timestamp has the correction for UTC at the end not sure if this is generating the error.
Thanks for any help.
To get expected results you should consider using DateTime():
<?php
echo date('D',strtotime("2013-06-16T06:00:00-07:00")) . "\n";
echo date('D',strtotime("2013-06-16T18:00:00-07:00")) . "\n";;
$dt1 = new DateTime("2013-06-16T06:00:00-07:00");
$dt2 = new DateTime("2013-06-16T18:00:00-07:00");
echo $dt1->format('D') . "\n";
echo $dt2->format('D') . "\n";
Output
Sun
Mon
Sun
Sun
Fiddle
This is because The Date represents the time is in time zone specified in date.timezone settings. So the timezone -07:00 is parsed and converted back to date.timezone timezone.
To understand the idea just add e in the date string
echo date('D e',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D e',strtotime("2013-06-16T18:00:00-07:00"));
See example.
Its better you use DateTime(). It does not have such limitation.
I am pulling a date value from a MySQL DB formatted as 01/20/13 I am calling the PHP date function on this value returned to get what day of the week it is, so 01/20/13 is today's date which is Sunday but it keeps returning the value Wednesday. I have included the code below I am new to programming so this is probably a stupid error I am overlooking.
<?php
require '../TimeCard/DB.php';
try{
$stmt = $conn->prepare('SELECT `date` FROM `timeRecords` WHERE `employeeID`= 1 ');
$stmt->execute();
} catch(PDOException $e){
echo'ERROR: ' . $e->getMessage();
}
while($row = $stmt->fetch())
{
echo date("l", $row['date']) . "<br>";
echo $row['date'] . "<br>";
}
?>
Mysql does not store dates as m/d/y it stores them as Y-m-d you're mysql database will turn "01/20/13" into 0000-00-00.
However, if you are not using the date type, and storing as a string use
strtotime($row['date'])
use strtotime on your mysql stored date, then use the date function on it
$day = date('l',strtotime($row['date']));
try with strtotime()
echo date("l", strtotime($row['date'])) . "<br>";
The second argument to PHP's date() function is an integer timestamp. You'd have better luck using DateTime, eg
$dt = DateTime::createFromFormat('m/d/y', $row['date']);
echo $dt->format('l');
This gives you the added bonus of tailoring the date parser to match your source format rather than relying on strtotime() which definitely has its quirks such as treating dates with forward-slashes (10/12/13) as US (12th October) vs dates with hyphens (10-12-13) as EU (10th December)
Example here - http://codepad.viper-7.com/iVXxA1
So I have a field in my database called 'DateTime' and the following lines of code:
echo "Date/Time: ";
echo $row['DateTime'];
How do I format it so that instead of being like this:'2013-02-07 22:14:56', it will be like this: '07/02/13 - 22:14'
Thanks.
Alternatively you could use:
DateTime::createFromFormat('Y/m/d H:i:s',$row['DateTime']); this will give you a datetime object, which are quite nice to work with.
Another alternative would be to have MySQL format the DATETIME value as a string in the desired format, using the DATE_FORMAT function.
SELECT DATE_FORMAT(`DateTime`,'%d/%m/%y - %H:%i') AS `DateTime`
...
No change required to your PHP code except for the SQL text sent to the database server.
This approach can very efficient, and reduce the amount of code you need, if all you are doing with this string is displaying it. If you are doing any sort of manipulation on this value, then casting the string value returned from MySQL resultset into a datetime object is probably a better way to go.
A demonstration of the DATE_FORMAT function:
SELECT DATE_FORMAT('2013-02-07 22:14:56','%d/%m/%y - %H:%i') AS `DateTime`
DateTime
----------------
07/02/13 - 22:14
how to output date into Year textbox Month textbox Day textbox
$book_date = $myrow["Publication_Day"];
$book_year = Date("Y", strtotime($book_date));
$timestamp contains ur date & time in any format.....................
date('Y/m/d - H:i',strtotime($timeStamp));
echo date('d/m/y H:i', strtotime($row['DateTime']));
See date and strtotime for more detail on the functions from the docs
$mytime = strtotime('2013-06-07 22:14:56');
$newDate = date('m/d/y - G:i', $mytime);
echo $newDate;
Here's an alternative using DateTime. If you're working with timezones this code can be easily modified to handle that.
$datetime = new DateTime('2013-02-07 22:14:56');
echo $datetime->format('d/m/y H:i');
See it in action