I am trying to figure out how to subtract 1 hour form the time being outputted. Our web server is 1 hour ahead of our time. I am using the following to return the time a table was created in our MySQL database.
ini_set('date.timezone', 'America/Los_Angeles');
$con = mysql_connect('localhost','database','password');
$db = mysql_select_db('database');
$sql = mysql_query("SHOW TABLE STATUS WHERE `name`=\"initm\"");
$foo = mysql_fetch_array($sql);
$ts = strtotime($foo['Create_time']);
echo "<h3>Last Updated ";
echo date('m/d/Y g:i a T', $ts);
echo "</h3>";
If I try to subtract time $ts = strtotime($foo['Create_time']-3600);
it returns Last Updated 12/31/1969 4:00 pm PST. I understand it is subtracting from the UNIX timestamp, which is defined as the number of seconds since January 1, 1970 and not from the time in the table.
I tried adding ini_set('date.timezone', 'America/Los_Angeles'); but it just changes the time zone outputted.
Any advice is greatly appreciated. A novice in PHP and MySQL.
strtotime assumes it's parameter is a string, and converts it to seconds. You need to do the subtraction after the conversion:
$ts = strtotime($foo['Create_time'])-3600;
SELECT UNIX_TIMESTAMP(Create_time) - 3600 ...
is far easier than multiple round-trips through PHP's and MySQL's date/time processing systems.
However, since strtotime is giving you invalid results, I'm guessing that whatever you're storing in Create_time is not actually a native mysql data/time value, but probably some wonky non-standard date value as a string. That means strtotime() is returning a boolean FALSE to indicate failure, and then you force PHP to convert that false to an integer 0 to handle your "minus 3600" calculation.
You should generally always store things in native formats/types, to prevent occurences such as this. Native allows you to use native facilities for processing. Custom formats mean pain/suffering
Why not :
$query = "SET time_zone = '-6:00'";
This will set your configuration in the same way as SET charset utf8.
Related
I am saving the timestamp in SQL as bigint(20). The number is correct and in android or https://www.epochconverter.com it works fine.
However I am not able to create a date-string based on the timestamp received from database.
First of all, the timestamp seems to come from database as a String, so I can't just say echo date("d.m.Y, $timestamp). The result is the famous 31.12.1969.
So I tried echo date("d.m.Y, strtotime($timestamp)). However, even though strtotime is said to be able to convert almost everything to a timestamp, a simple String containing a timestamp is not possible. Results still remained on the last day of Brian Adams probably favorite year.
Some progress I made by casting the $timestamp to a float value like so: echo date("d.m.Y", floatval($timestamp));. However, now things got really confusing for me. I seemed to have successfully converted my timestamp, however, date() gave me the dates around 02.09.52299.
The timestamps I am using are timestamps of current time, e.g. 1588489252657, which currently leads to the date 23.03.52307.
So all I want is to come to a date based on the timestamp 1588489252657 to see the 03.05.2020 on my screen.
Thanks for any advice!
<?php
$timestamp = 1588489252657; // Timestamp in Milliseconds
$seconds = round($timestamp/1000, 0); // Timestamp Rounded to Seconds
$date = new DateTime(); // Create PHP DateTime Object
$date->setTimestamp($seconds); // Set the Timestamp
echo $date->format('Y-m-d H:i:s'); // Specify the Required Format
The answers are pretty much in the comment sections. But I have shared this answer since this is another approach in OOP fashion. You can leverage the power of PHP's DateTime Class.
PHP Official Documentation For DateTime Class Link Below:
PHP DateTime Class
You have to transform the timestamp to seconds first.
$timestamp = 1588489252657;
$dateInUnixSeconds = round($timestamp / 1000, 0);
$date = \DateTimeImmutable::createFromFormat('U', (string) $dateInUnixSeconds);
echo $date->format('d.m.Y');
PS:
I recommend you to use the \DateTimeImmutable object to avoid mutability problems.
https://github.com/Chemaclass/php-best-practices/blob/master/technical-skills/immutability.md
I have a table containing a datetime column:
$db = new SQLite3('test.db');
$results = $db->query('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT,
date DATE, foo TEXT);');
and I add a row (storing the datetime of the addition of the row in UTC) with
$results = $db->query('INSERT INTO test (date, foo) VALUES(CURRENT_TIMESTAMP, "bar");');
This works. Now when displaying the rows:
$results = $db->query('SELECT * FROM test ORDER BY date desc');
while ($row = $results->fetchArray()) {
echo $row['date'];
}
the date is displayed like this, in UTC: 2019-04-27 16:41:33.
How to display it in the local timezone instead? (including Daylight Saving)
I can imagine there are different options:
store directly in SQLite with local timezone (but I think this is not good practice)
store in SQLite in UTC, and do the UTC->local timezone conversion during the SELECT. How?
store in SQLite in UTC, and do the UTC->local timezone conversion via PHP.
How to do this properly?
As suggested by a comment, this page mentions the localtime modifier. Here is a solution:
$results = $db->query('SELECT datetime(date, "localtime") AS localdate, foo FROM test ORDER BY date desc');
while ($row = $results->fetchArray()) {
echo $row['localdate'];
}
I would say what you need to do is store you time values as something called a "Unix timestamp", which is literally the number of seconds since UTC 1st January 1970 00:00. You can get the current time by using PHP's time() function. Then change you DB "date" column to an integer type. In this way, the time data stored in your database is completely independent of timezone etc, which is a good thing!
To interpret these timezones properly we can use PHP DateTime object, which will do all the heavy lifting in terms of timezones and DST. The following code snippet gives the basic ideas:
// These need to be provided from the client side
// Native JS will get you the offset, read my comment below for where to get DST from
$TimeOffset = $_POST['TZOffset'];
$isDST = $_POST['isDST'];
// DST needs to be an integer
if ($isDST == 'true'){
$isDst = 1;
}else{
$isDst = 0;
}
// Will give a result like Europe/London
// In most use cases, save this into the users session :)
$TimeZoneName = timezone_name_from_abbr('', $TimeZone * -60, $isDst);
// Now to tell PHP we are working in this timezone
date_default_timezone_set($TimeZoneName);
///// Somewhere else in your script
// Fetched a unix timestamp from your DB; $timestamp
$DT = new DateTime();
$DT -> setTimestamp($timestamp);
// Now you have control over how it is displayed
// For instance, this gives a 2010-04-28 22:41:43 type format
// This will be correct to the user's timezone we calculated earlier
echo $DT -> format('Y-m-d H:i:s');
Whether the client is in DST should obtained from JS, have a look at the answer for this question for a simple method, but many libraries etc can also do it.
So the big question, why go the long way around?
The time data in your database is completely independent of DB/server configuration. Over time these can wander and bugs can come into play; you want your fundamental data to be consistent.
It lets PHP do all the hard work regarding adjusting for timezones and DST, across every region / DST regime. It becomes a big headache quickly if you try and solve this yourself, DST is very awkward.
Time maths is much more efficient and easier, especially in DB calls. All you need to do is numerical comparisons of seconds, rather than dealing with SQL functions etc etc.
No reliance on specific DB functions or engines; its just an integer value! Makes for much easier portability.
A word of caution, be careful of the maximum value in your DB integer column. Find the max/min values, convert them to date times (this is a useful tool) and check against your use case.
The final thing to say, is you can use the DateTime object to interpret time strings (as you are using now) in UTC, then set the timezone before printing them. It will work, but I feel it is much more prone to error.
I hope that helps :)
Look at this SQL solution:
select ts,
cast( round( ( julianday( ts, 'localtime' ) - julianday( ts ) ) * 24 ) as int ) as diff,
datetime( ts, '+' || cast( round( ( julianday( ts, 'localtime' ) - julianday( ts ) ) * 24 ) as int ) || ' hours' ) as local
from test
Result:
ts dif local
2020-03-25 14:09:31 1 2020-03-25 15:09:31
2020-03-31 06:54:08 2 2020-03-31 08:54:08
2020-03-31 14:08:10 2 2020-03-31 16:08:10
2020-04-01 07:23:04 2 2020-04-01 09:23:04
2020-04-01 09:53:19 2 2020-04-01 11:53:19
How would i turn
2012-04-11 12:49:14
into a unixtime stamp?
I have tried
$time = mktime("2012-04-11 12:59:14");
and
$time = strtotime("2012-04-11 12:59:14");
EDIT
Basically on update my database adds a date/time that looks like this.
2012-04-11 12:49:14
I need it to be turned into a unix timestamp so i can use a "time ago" function i found.
My tests have revealed,
Database input -> 2012-04-11 13:22:05 which is converted into -> 1334143355 -> But the current time from(time()) is ->1334146956
I dont see why they do not match up?
The statement $time = strtotime("2012-04-11 12:59:14"); is working just fine.
Do a
echo $time;
after your declaration.
It sounds like an issue with mismatched time offsets (daylight saving perhaps). It is always best to do it all in PHP or all in MySQL to avoid mismatched time offsets.
1334146484 - 1334142872 = 3612s = 1h 12s
You should use UNIX_TIMESTAMP either when inserting or retrieving the data depending on whether you prefer storing as unix timestamp or datetime -
INSERT INTO `table` (date_field) VALUES (UNIX_TIMESTAMP('2012-04-11 12:59:14'));
SELECT UNIX_TIMESTAMP(date_field) FROM `table`;
I expect that the difference is from being in a different timezone. The difference is more or less +1 hour form the expected result. You need to be more specific about what time you want - as in are you recording/retrieving time from your timezone, or from UTC.
$time = strtotime("2012-04-11 12:59:14"); is correct option.
Currently I store the time in my database like so: 2010-05-17 19:13:37
However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041. (These two times are different)
So how could I convert the timestamp to unix timestamp? Is there a simple php function for it?
You're looking for strtotime()
You want strtotime:
print strtotime('2010-05-17 19:13:37'); // => 1274123617
Getting a unixtimestamp:
$unixTimestamp = time();
Converting to mysql datetime format:
$mysqlTimestamp = date("Y-m-d H:i:s", $unixTimestamp);
Getting some mysql timestamp:
$mysqlTimestamp = '2013-01-10 12:13:37';
Converting it to a unixtimestamp:
$unixTimestamp = strtotime('2010-05-17 19:13:37');
...comparing it with one or a range of times, to see if the user entered a realistic time:
if($unixTimestamp > strtotime("1999-12-15") && $unixTimestamp < strtotime("2025-12-15"))
{...}
Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:
if(ctype_digit($_GET["UpdateTimestamp"]))
{...}
If you're using MySQL as your database, it can return date fields as unix timestamps with UNIX_TIMESTAMP:
SELECT UNIX_TIMESTAMP(my_datetime_field)
You can also do it on the PHP side with strtotime:
strtotime('2010-05-17 19:13:37');
if you store the time in the database, why don't you let the database also give you the unix timestamp of it? see UNIX_TIMESTAMP(date), eg.
SELECT UNIX_TIMESTAMP(date) ...;
databases can also do date and time comparisons and arithmetic.
I have a mySQL database with a timestamp field. It currently only has one entry while I'm testing, it is
2010-02-20 13:14:09
I am pulling from the database and using
echo date("m-d-Y",$r['newsDate'])
My end result is showing as
12-31-69
Anyone know why?
Edit:
editedit:
disregard that edit... the FTP addon for notepad++ timed out and unfortunately doesn't display an error when it can't synch.
The date function expects an UNIX timestamp as its second parameter -- which means you have to convert the date you get from the DB to an UNIX timestamp, which can be done using strtotime :
$db = '2010-02-20 13:14:09';
$timestamp = strtotime($db);
echo date("m-d-Y", $timestamp);
And you'll get :
02-20-2010
You were passing the '2010-02-20 13:14:09' string to the date function ; that string is not a valid UNIX Timestamp.
'12-31-69' is probably 1970-01-01, in your locale ; and 1970-01-01 is the Epoch -- the date that corresponds to the 0 UNIX Timestamp.
For starters, the php date() function is expecting seconds as the second variable. So that accounts for why your date is displaying wrong. Check this source on that issue.
Which then provides us the answer to the problem, to get PHP to format the date from a SQL timestamp correctly, we just change the query a tad...
SELECT author, `when`
Change it to...
SELECT author, UNIX_TIMESTAMP(`when`)
Then use the PHP date function, with the variable that is storing the result of that above SQL query.
You could just use MySQL's date_format() function instead:
SELECT date_format(timestampfield, '%m-%d-%Y') FROM table etc....
This will save you having to round-trip your timestamp into unix time and then back into a normal date string in PHP. One datetime formatting call rather than two.
i think this will be useful to newble:
example basic subtraction 1 hour from date from MYSQL format:
$to='2013-25-10 22:56:00'; //curr time
$timestamp = strtotime($to); //convert to Unix timestamp
$timestamp = $timestamp-3600; //subtract 1 hour (3600 this is 1 hour in seconds)
echo date("Y-m-d H:i:s",$timestamp); //show new date
EDIT: After checking, it appears that MySQL returns a timestamp as a string to PHP, so this answer was bogus :)
Anyway, the reason you get a date in 1969 is probably that you're converting a zero unix time from UTC to localtime. The unix time is the number of seconds since 1970. So a value of 0 means 1970. You probaby live in a timezone with a negative offset, like GMT-6, which ends up being 31-12-69.
ok, I was wrestling with this for a week (longer but i took a break from it).
I have two specific fields in tables
creationDate > timestamp > current_timestamp
editDate > timestamp > current_timestamp
they were pulling out either dec 31 1969, or just nothing... annoying... very annoying
in mysql query i did:
unix_timestamp(creationDate) AS creationDate
unix_timestamp(editDate) AS editDate
in php convert i did:
$timestamp = $result_ar['creationDate'];
$creationDate = date("Y-M-d (g:i:s a)", $timestamp)
echo($creationDate);
$editstamp = $result_ar['editDate'];
$editDate = date("Y-M-d (g:i:s a)", $editstamp)
echo($editDate);
this solved my problem for me returning
2010-Jun-28 (5:33:39 pm)
2010-Jun-28 (12:09:46 pm)
respectively.
I hope this helps someone out..