MySQL custom Timestamp value - php

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!

Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Related

EPOCH Unix MYSQL Query

I'am using a script that saves some fields into my database as Epoch & Unix Timestamp format, (I think because the field is submitting as something like 1515469971), the column is an "int" format type.
I want to know, how I can make a query, to search all the inputs that have created on the current month.
I am so confused about it, hope you can help me :)
Firstly, show me your table structure with show create table table_name.
If you're using an int to store the time. There are two ways to do that:
Convert the field to a date string with from_unixtime. Then you got a string like '2018-01-08 05:05:05', and just compare the month value.
Calculate the very beginning and the end of the current month in advance. Then just select * from table_name where time >= beginning_of_the_month and time <= end_of_the_month.

selecting rows from database between two dates giving wrong results

Selecting rows from database between two dates giving wrong results, the below query not working for me. I tried some answers, but this one not giving the correct results.I think, i am missing somewhere.
SELECT * FROM Table WHERE Date BETWEEN '07/10/2015' AND '07/14/2015'
changed to
SELECT * FROM Table WHERE Date BETWEEN '07-10-2015' AND '07-14-2015'
still not working!
That's right, you can not use BETWEEN statement when the data type format is not a DATE or DATETIME, you must change the data type first.
BTW I just realized that even data type is a DATE/DATETIME format you can't use / in the SQL statement itself when using MySQL, versus SQL you can use / when the column data type is DATE/DATETIME. Just correct me if I'm wrong...
Use the default date format YYYY-MM-DD
SELECT * FROM your_Table
WHERE Date BETWEEN '2015-07-10' AND '2015-07-14'
Erm, you're searching on "Date" but are you meant to be searching on "CreateDate"? That's what your image shows.

error in save date in database with php

want to be able to store the current date from PHP in to a mysql table.
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."','".$date."','".$yes."')";
Date's column type in database is DATE.
but date save in database like this: 0000-00-00
help me.
try this
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."',".$date.",'".$yes."')";
or
if you want to inset today's date use
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
I need to store Date field in database for example VARCHAR(10) becouse, DATE type in mysql server don't support persian (farsi) date.
If you are getting the current date, just use one of the built-in MySQL functions for that:
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
You won't ever have to worry about formatting or any sort of funny inputs and you can still do all sorts of addition, subtraction and the like using one of the many other functions.
Edit: I have just looked up some information on the date system and you will run into problems trying to squeeze that data into a date field. The first six months have 31 days, the next 5 have 30 days, then the last has 29 - unless it is a leap year in which case it has 30. The date field simply won't LET you insert these values into a date field.
You might need to convert your data to a timestamp and write a function to encode/decode it into the format you need.

PHP MySql: how save timestamp UTC, like "Flickr"?

For default i'm using this snippet in my codes
setlocale(LC_TIME,'it_IT');
and I did save all my dates in mysql in a timestamp format.
In a view of make international one of my codes, i would like to save in MySQL a similar value
2013-07-12T07:59:27+0000
but of course with the +2 hours. So a Polish user will have a +3 and so on...
What's the best field for mysql to store that value? I need also to work with date, from PHP and/or from directly MySQL (for example)
SELECT id WHERE data BEETWEN [...]
Of course if I start with correct way, i don't need to change in future all my dbs, codes, etc...
Thank you very much!
For me is better store date time as UNIXTIME, it avoid the time difference between user from different locations.
You can use:
SELECT * FROM table_name
WHERE field_name
BETWEEN UNIX_TIMESTAMP('2013-07-12 07:59:27')
AND UNIX_TIMESTAMP('2013-07-13 07:59:27')
to get the Date Range

How to hide records when the date has passed?

I am unsure on how to check whether a date has passed in order to only show rows where the date has not passed.
I am using CodeIgniter and when the date is set, I'm not using a date type (using varchar) in the MySQL table as I'm using the jQuery date-picker to set the date.
So when the date is set, I need to pull the date from the table to check whether the date has passed or not?
So I'm not sure whether I have to totally change my date input to use the date type in the MySql table in order to pull the date and check whether it is < now()
Any guidance or advice would be extremely appreciated :)
Is there any particular reason for NOT using the date type in MySQL? If is a date, why not store it as a date? It will still be usable as a string in your PHP script when you retrieve it from the database.
You should always use the date type in MySQL if you are tracking dates, that way you have all the date functions available to you. The date functions of MySQL are much faster than trying to parse them as text with PHP.

Categories