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..
Related
I am using a datepicker within a form which returns the date format F jS, Y (e.g. November 6th, 2013). The mysql database is set up to use Unix Timestamp which is a 10 digit format. I am now looking for a way to pass the form field value (which is November 6th, 2013) in a unix timestamp format into the database, but unfortunately it wont work.
Here is my controller code:
$insertData['enddate'] = $this->input->post('openDays');
I tried the following:
$oldenddtime = $this->input->post('openDays');
$newenddate = int variant_date_to_timestamp ( $oldenddtime );
$insertData['enddate'] = $newenddate;
The db result now is 0... Any idea of what I am doing wrong here?
Once this is resolved, I would need to hour within the timestamp to be ALWAYS 5pm on the selected date...
Thanks in advance ;)
Why not use strtotime? Should do exactly what you need and convert the string to a unix timestamp.
I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime
I'm trying to convert an epoch timestamp with php 5.3 with the following statement
date('d-m-Y',strtotime('1349042399999'));
to human readable format and getting wrong result: 01-01-1970what should return30-09-2012. I have been searching around and founding the following topic PHP strtotime returns a 1970 date when date column is null but did not help on my case.
The reason for that is that there are milliseconds embedded in that timestamp, which causes it to go over the integer overflow limit.
chop the last 3 characters, and you're good to go:
$original_timestamp = "1349042399999";
$timestamp = (int) substr($original_timestamp,0,-3);
echo date('d-m-Y',$timestamp);
By using strtotime, you are trying to convert a timestamp into another timestamp. Your timestamp is also in microseconds. The date function just needs the timestamp without the microseconds like so:
date('d-m-Y',substr('1349042399999', 0, -3));
I believe that the Second Rikudo solution worked because of the cast to int, not because of trimming the milliseconds.
It seems to me that this should work for you (it did for me)
$original_timestamp = "1349042399999";
date('d-m-Y', (int) $original_timestamp);
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.