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.
Related
I have some php code dynamically generating a .ics file for download. I am passing in a start date value of 08/01/2019 and when it succesfully gets imported into ical / google calendar it is off by one day, despite the epoch timestamp being correct. I am not sure what I am missing here.
I have tried reformatting the date to a different date string, than converting it to the format required for ical and that results in jan 1 1969. I am using the WordPress date_i18n() function to format the date in expected value.
$dateVal; // contains 08/01/2019
$dtstart = date_i18n("Ymd\THis\Z", strtotime( $dateVal) );
// output: 20190801T000000Z
When the event goes into the calendar, it shows my date as july 31st, 2019 at 6pm NOT the expected value of august 1st, 2019
Any ideas how I can debug this?
What I think is happening is this:
Your event is supposed to be at 8/01/2019 in your timezone, which based on the apparent offset seems to be UTC+6.
the \Z in your format string is indicating that the event is at 8/1/2019 in UTC, so when you see it on your calendar, it is adjusted to your timezone, so it gets 8 hours subtracted.
Try leaving off the \Z, I think it should just use your local timezone.
Or convert the time to UTC.
$date = new DateTime($dateVal);
$date->setTimezone(new DateTimeZone('UTC'));
$dtstart = $date->format('Ymd\THis\Z');
cI use jQuery calendar date picker on my form. When the date is not filled, it always shows "1969-12-31" value. I did not want to show this value, 0000-00-00 is fine for me.
My MySQL date column is receive_dt DATE NOT NULL,
This is a snippet code from the PHP file to handle the form.
...
$rcv_dt = $_POST['receive_dt'];
list($year,$month,$day)=explode('/',$rcv_dt);
$timestamp=mktime(0,0,0,$year,$month,$day);
$receive_dt=date('Y-m-d',$timestamp);
..., receive_dt) VALUES (....,'$receive_dt')...
I've tried to do the strtotime() but no luck.
$receive_dt=date('Y-m-d', strtotime($rcv_dt));
I've even changed the MySQL reveive_dt column to DATE NULL, but still no luck.
Firstly, the fact that you're getting the end of 1969 rather than the beginning of 1970 (the "Unix epoch" begins at midnight on 1st Jan 1970) suggests you have some timezone-handling bug causing you to "lose" an hour, so just a heads-up on that.
Now, the reason you're seeing this at all, is that PHP's date formatting functions treat whatever input you give them as a number; if you give them an empty string, or null, this will be converted to the number 0, and interpreted as the beginning of the Unix epoch - 1st Jan 1970. MySQL will probably do something similar if you try to pass it an empty string or 0 when populating the column.
What you need to do is specifically detect this case - easy enough if your application should never actually have 1st Jan 1970 as input - and specifically insert a NULL into the database rather than formatting the date.
$invalid_dates='1969-12-31'; // anything before
$rcv_dt = $_POST['receive_dt'];
if(strtotime($invalid_dates) >= strtotime($rcv_dt))
{
$rcv_dt='0000-00-00'; // or $rcv_dt=date('Y-m-d'); // today
}
http://php.net/manual/en/function.checkdate.php
Learn to use the DateTime object. Using strtotime, mktime, and other integer based time formats is an outdated and bad approach.
$dt = date_create($_POST['receive_dt']);
if ($dt !== null)
{
echo $dt->format('Y-m-d H:i:s'); // insert this value
}
if you send $receive_dt as an empty string ..., receive_dt) VALUES (....,'')... (after checking the post variable is empty) then mysql will treat is as a null, otherwise php is sending a date of 0 which for mysql is the start date of the unix epoch.
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 have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');
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..