I have a plugin that allows adding dates from admin. I am trying to add some data into the database by running queries but I can't quite figure out how it's handling the dates. I know the plugin is saving the dates in (INT).
Example: This is the format used for generating the dates in wp-admin
12-01-2021 23:15 +0300
then end up in MySql database as (INT) value of 1610482500
Basically, all I need is to find how I can convert dates in excel or PHP to convert the dates to integers. I tried Excel Date to Number Conversion but it generates about 5 digits only. Not sure how the date is converting so any clues will be very helpful
Thanks
I have found a way.
<?php
$datetimeStr = '2021-01-12 23:15 +0300';
$datetime = strtotime($datetimeStr);
//Displays 1610482500
echo $datetime;
?>
Related
I'm using Access DB 2007 - 2010; I've tried to import many CSV files but the timestamp column keeps failing to import correctly.
So I linked all of the CSV's to an Access DB and I'm trying to query all of the tables.
I'm trying to extract the year and day of the year from the time stamp (which is currently a string)
I'm trying to combine the Format with datepart functions and it keeps failing. (it just says error in the table)
The format function by itself works but I can't combine it with anything.
I'm basically trying to do this:
select datepart("y", Format(gmt, "dd-mmm-yyyy hh:nn:ss")) as DOY from Table1;
but it fails. I've also tried CDate and DateValue in different combinations but it all fails.
Does anyone know how to get this to work?
UPDATE
The format function isn't doing anything. The text remains the same no matter how I try to format it.
Here's a datetime sample: 05-Dec-2008 13:40:01.955
Access can't cope with the milliseconds in your date strings.
Use Left() to exclude them and feed the resulting substring to CDate().
SELECT CDate(Left(gmt, 20)) AS date_from_string
FROM Table1;
Once you have a valid Date/Time value, you can use Year(<Date/Time value>) or DatePart("yyyy", <Date/Time value>) to extract the year. And DatePart("y", <Date/Time value>) will give you the day of the year.
Just solve this issue, here is my code for your reference:
update tablename
set date=cdate(format(left(gmt,4)&"-"&right(gmt,2),"yyyy-mm"))
Been searching for ages to try and find an answer for this but got no luck.
I have a column with a Datetime type. I have a field on a webpage which has an input control which puts the date in this format 20-12-2015 23:30 .When I try and store this I just get blank entries or 0000-00-00 00:00:000
The field likes the date in this format 2015-20-12 23:15:00
Can anybody help me please help me work out how to convert this correctly. I have been trying with several Date_Format methods but all give me an error
"1292 Incorrect datetime value: '10/12/2015 10:50'"
If you have a fixed format date being returned from the page that is not a compatible date, and you cannot change the format used on the page, then before you attempt to store it on the database you need to convert it to a compatible format.
PHP has a DateTime object that does exactly this
<?php
$in = '20-12-2015 23:30';
$date = DateTime::createFromFormat('d-m-Y H:i', $in);
$to_db_date = $date->format('Y-m-d H:i:s');
echo $to_db_date;
This produces a MySQL compatible date string like
2015-12-20 23:30:00
I have DATE type column in a MySQL database table with dates stored in it. I am using the YYYY-MM-DD format when I upload the dates to the database.
When I check the values they are stored in the correct and expected format (2005-01-02), however when I am attempting to read these values with my C++ program I am receiving something like this: 02/01/2005 00:00:00
I don't really understand the reason, I thought in the DATE type only the YYYYMMDD gets stored without the time. The other weird thing is the reformatting I thought YYYY-MM-DD is an accepted format.
Here is my code:
String^ getDateOfBirth = myReader->GetString("player_date_of_birth");
DateOfBirthLabel->Text = getDateOfBirth;
I am using Visual Studio 2013.
Any help would be appreciated.
I am new to Cassandra cql (cqlsh 4.1.1, Cassandra 2.0.8.39, CQL spec 3.1.1, Thrift protocol 19.39.0) - using the cql COPY command to a table from a CSV formatted file and I get the following error: Bad Request: unable to coerce '2012/11/11' to a formatted date (long). How do I change a column using cql so that it accepts the date from my CSV file?
as Brian said, there are CQL timestamp type to follow to get CQL query running. Sometimes it looks like quite weird indeed ! I've got the same issue few weeks ago with a date time insert like this one :
INSERT INTO my_table (id,lastvisitdate) VALUES (1682221,'2012-03-25 02:26:04');
I got this error : Bad Request: unable to coerce '2012-03-25 02:26:04' to a formatted date (long) ! mmmm... so bad as the date time seems to be correct !
After many tries and before going nuts, I've just added a Z at the end of the time, Z stands for Zulu time which is also UTC and GMT :
INSERT INTO my_table (id,lastvisitdate) VALUES (1682221,'2012-03-25 02:26:04Z');
Yessss ! It works ! So do not forget the timezone in your date time values, it could be helpful ! ;-)
There is not a direct way to do that from within CQLSH.
There are a certain set of string date formats which can be coerced. See the CQL Timestamp type documentation page for some examples like:
yyyy-mm-dd HH:mm
yyyy-mm-dd HH:mm:ss
yyyy-mm-dd HH:mmZ
yyyy-mm-dd HH:mm:ssZ
yyyy-mm-dd'T'HH:mm
yyyy-mm-dd'T'HH:mmZ
yyyy-mm-dd'T'HH:mm:ss
yyyy-mm-dd'T'HH:mm:ssZ
yyyy-mm-dd
yyyy-mm-ddZ
As a workaround you could modify your CSV file to adjust the date format, then import it. (In your case it may be as simple as "yyyy/mm/dd" -> "yyyy-mm-dd".)
If I have a date such as mm/dd/yyyy. How can I get mysql to actually store the date in that format. Is this possible or do have to store it in the form of yyyy/mm/dd and convert it later?
What I want to do is insert a date in the mm/dd/yyyy format but the database will not allow it. It wants yyyy/mm/dd
What is your reason for doing this? I can't see any reasonable use for it.
You cannot change the way MySQL stores dates, no. But you can of course format them when reading/writing them. You can do it in SQL query like this:
For example you can use STR_TO_DATE function to format the date when inserting it:
INSERT INTO mytable (mydate) VALUES (STR_TO_DATE('12/31/2009', '%m/%d/%Y'))
And vice versa:
SELECT DATE_FORMAT(mydate, '%m/%d/%Y') FROM mytable /* returns 12/31/2009 */
But as FactalizeR pointed out, it is not a good practice to do it in the query and it should be moved to script, like this (considering you are using PHP).
$date = '12/31/2009';
$date = date('Y-m-d', strtotime($date));
mysql_query("INSERT INTO mytable (mydate) VALUES ({$date})");
And vice versa
$date = mysql_result(mysql_query("SELECT mydate FROM mytable"), 0, 0);
$date = date('m/d/Y', strtotime($date)); //returns 12/31/2009
There is a built-in DATE type in MySQL. It allows to store a date. And later in your programming language you extract this date and convert to whatever format you like. Also, conversion can be done directly in MySQL via DATE_FORMAT(date,format) function.
MySQL DATE fields must store the date in YYYY-MM-DD (including dashes) format. If you attempt to store it any other way you will have problems with date comparisons and ordering.
Conversion later is a trivial task. Is there a compelling reason why you are trying to avoid doing this? What other technology are you using to talk to the MySQL database? (i.e. PHP, C# etc...)
I think you're mixing up what you are storing - the date itself - with how that date is subsequently referred to.
Why do you want to store in a particular format? Do you want to insert in that format? In which case you might be able to get away with it, depending on the localisation of your install, but otherwise convert - insert would be the way to do it (depending, of course, on how you're inserting).
Are you creating a SQL command from strings?