How to create a SQLite Timestamp value in PHP - php

I want to generate a timestamp in PHP and then store that value in SQLite.
Here is the code:
$created_date = date("YYYY-MM-DD
HH:MM:SS",time());
Here is what gets stored in the DB (which looks wrong):
11/21/0020 12:39:49 PM
How should I change my code to store the current date/time properly in SQLite

Why not just update your SQLite query to use date('now')? reference
But, within PHP you should be able to use
$created_date = date('Y-m-d H:i:s');

I just implemented this for my own project..
My solution was to use the UTC time format officially specified for the web: RFC3339.
You can use any of the following PHP time format constants:
DATE_RFC3339
DATE_W3C
DATE_ATOM
Example:
$sqlite_timestamp = date(DATE_RFC3339);
echo "My SQLite timestamp = ".$sqlite_timestamp;
The best part is the alphanumeric order corresponds to the date order, so you can use ORDER BY SQL statements on the date fields!

How do you check what the stored value is? Also, what is the datatype of the column in the database? Are you aware that SQLite is typeless, so all values are being stored as strings or integers?

$created_date = date('Y-m-d H:i:s');
echo $created_date ."<br />\n";
$created_date = date('l, F jS, Y - g:ia');
echo $created_date ."<br />\n";
$created_date = date('n/j/y H:i:s');
echo $created_date ."<br />\n";
$created_date = date('r'); // RFC 2822 formatted date
echo $created_date ."<br />\n";
$created_date = date('c'); // ISO-8601 formatted date
echo $created_date ."<br />\n";
Output could be:
2011-04-01 19:33:40
Friday, April 1st, 2011 - 7:33pm
4/1/11 19:33:40
Fri, 01 Apr 2011 19:33:40 +0200
2011-04-01T19:33:40+02:00
Be aware that date() function depends of default timezone set in PHP config. You can always test it using echo date_default_timezone_get(); or set it by date_default_timezone_set('TIMEZONE_IDENTIFIER'); where TIMEZONE_IDENTIFIER is one from the list of supported timezones.

Related

PHP Date conversion date_create_from_format() not returning expected result

I have a string: 30/06/18 (30th June 2018)
I am converting to a date:
$calcFieldDate = date_create_from_format('d/m/y', '30/06/18')->format('d-m-Y');
echo $calcFieldDate;
Result: 18-06-2018
Now I want to add 20 days to the date:
$expiryDate = date("d-m-Y", strtotime("+20 days", $calcFieldDate));
echo $expiryDate;
Expected Result: 08-07-2018
Actual Result: 31-01-1970
I am obviously creating a date format which is then subsequently being treated as a string...
Every time I try a conversion, I just hit another road block - is there anyway to create a date that is then treated like a date?
$calcFieldDate = date_create_from_format('d/m/y', '30/06/18')->format('d-m-Y');
echo $calcFieldDate;
Result:30-06-2018
$expiryDate = date("d-m-Y", strtotime("+20 days", strtotime($calcFieldDate)));
echo $expiryDate;
Result:20-07-2018
Strtotime() The second parameter is the timestamp
You actually don't need to revert using strtotime and date functions, you can actually use DateTime to simply add dates into it:
$calcFieldDate = date_create_from_format('d/m/y', '30/06/18');
echo $calcFieldDate->format('d-m-Y'); // get inputted date
$expiryDate = clone $calcFieldDate; // clone the original date object
$expiryDate->modify('+20 days'); // adjust the cloned date
echo $expiryDate->format('d-m-Y'); // show the adjusted date
This will sort your problem.
$str="30/06/18 (30th June 2018)";
$arr_temp=explode(" ",$str);
$str_date=str_replace("/","-",$arr_temp[0]);
$dt = DateTime::createFromFormat('d-m-y',$str_date);
$date=$dt->format('d-m-Y');
$new_date=date('d-m-Y',strtotime("+20 days",strtotime($date)));
echo $new_date;

How to convert date time to unix command date

How do I convert 2014-10-10 04:13:24 to 2014-10-10T04:13:24+00:00 in php or mysql
The above mentioned date is for xml format in sitemaps
For reference check Q: How do I compute lastmod date? (Sitemaps.org FAQ)
code which i have tried:
echo "GIVEN DATE ".$timestamp = "2014-10-10 04:13:24";
echo "<br>";
$year = date('Y',strtotime($timestamp)).";
$month = date('m',strtotime($timestamp)).";
$day = date('d',strtotime($timestamp))."";
echo '<br>';
$hour = date('H',strtotime($timestamp))."";
$minutes = date('i',strtotime($timestamp))."";
$seconds = date('s',strtotime($timestamp))."<br>";
$gmktime= gmmktime($hour,$minutes,$seconds,$month,$day,$year)."<br>";
echo "output date".$isodate = date('c', $gmktime);
is the above out put conversion correct?
**OUTPUT**
GIVEN DATE : 2014-10-10 04:13:24
output date : 2014-10-10T06:13:24+02:00
Your output is correct in the light of the Sitemaps.org spec. "2014-10-10T06:13:24+02:00" is the same date/time as "2014-10-10T04:13:24+00:00".
Learn more about the W3C Datetime encoding which is used by Sitemaps.org.
Also, don't solve this with date functions, solve this with string function / operations: You change a single byte inside a string at a fixed position and then you append a string:
$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "+00:00";
echo $timestamp, "\n"; // 2014-10-10T04:13:24+00:00
Or if you like to save some bytes in your file use "Z" instead of "+00:00" to denote the timezone:
$timestamp = "2014-10-10 04:13:24";
$timestamp[10] = "T";
$timestamp .= "Z";
echo $timestamp, "\n"; // 2014-10-10T04:13:24Z
PHP 5:
<?php
// Set the default timezone
date_default_timezone_set('UTC');
// Get Unix timestamp for a date
// Reference: mktime(hour, minute, second, month, day, year)
$time = mktime(04, 13, 24, 10, 10, 2014);
// Alternatively (must be a valid English date format)
$time = strtotime('2014-10-10 04:13:24');
// ISO 8601 date (added in PHP 5)
$isodate = date('c', $time);
echo $isodate; // 2014-10-10T04:13:24+00:00
// RFC 2822 formatted date
$rfcdate = date('r', $time);
echo $rfcdate; // Fri, 10 Oct 2014 04:13:24 +0000
?>
References:
date() / gmdate()
mktime() / gmmktime()
strtotime() : Supported Date and Time Formats
date_default_timezone_set() : List of Supported Timezones
ISO 8601
RFC 2822
In PHP
In PHP you use strptime() to parse the time and turn it into a structured array. Then pass the results of that into the mktime() function to get a UNIX timestamp.
In MySQL
Use UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(datetime_column) FROM table;

Format date output of time stamp in php

I'm using the following php command in displaying the time stamp in the table of my database system (postgresql) on a webpage
while ($column = pg_fetch_array($result)) {
echo "<tr>";
echo "<td>".$column[0]."</td>";
echo "</tr>";
}
However the time stamp format is too detailed and how could I simplify it display format for example just '2014-04-18 18:29'
The current output is something like 2014-04-18 18:07:36.978
Thank you in advance for every help.
In case you have a timestamp such as Thu, 21 Dec 2000 16:01:07 +0200 (or similar) you can use date and strtotime:
echo date('Y-m-d H:i:s', strtotime($column[0]));
Alternatively, if you use PHP 5.2+, you can use the DateTime class:
$dateTime = new DateTime($column[0]);
echo $dateTime->format('Y-m-d H:i:s');

strtotime returning wrong day

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.

php convert timestamp to xml datetime

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>';

Categories