I guess I have it partially working, but it inserted a random year,day & month rather than the actual date now.
This is in my "Post Article" .php file.
<?php
$date = date('Y-m-d', $timestamp);
What I have setup for my SQL table is this:
however it ends up showing this date:
any ideas?
If you don't pass a second parameters to date() it defaults to now.
$date = date('Y-m-d');
It would be even easier to do this in your SQL (and simultaneously make your PHP code simpler and easier to maintain). You can use several MySQL functions like NOW() and CURDATE():
INSERT INTO tablename (date) VALUES(NOW())
INSERT INTO tablename (date) VALUES(CURDATE())
FYI, I can't say for sure since I can't see your code, but I suspect $timestamp doesn't exist in your code. That will cause you to get the value you see in your database.
Related
basically I am using php and mysql to extend the functionality of a mailing client, data is sent over to my server using curl and then inputted into table A in my database, I then select all of the data from table A when it matches today's date and insert it into table B if the dates match like so:
$date = $_POST['dateAdded'];
$today = date("Y-m-d");
if ($date === $today) {
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="INSERT INTO `$tableB` (`$fields`) SELECT * FROM $tableA WHERE date_Added='$today'";
$link->exec($sql);
There is other code that is ran within this IF statement but I do not feel it is relevant to the question. I know there are other issues with this code such as not escaping post values etc but that is something I am going to tackle in the near future. The problem is that whenever this code is run as part of a cron job it only inserts one row into $tableB but when I clear tableB and one value matching $today from tableA and re-run the code it works everytime (i.e. if I run it manually), the only time it doesn't work is through the cron job. If there is any more code that is needed I am happy to provide it and any help is welcome.
The cron job is run using curl as the data is on a different server.
Could this be to do with the way curl is sending data initially through the cron?
Or is it a problem with the SQL statement itself?
Thanks.
be carefull, is de date_Added a date field or timestamp? in MySQL, is not the same, because your date("Y-m-d") against a timestamp is gonna be messed up and problably there lies your problem
try to change
$today = date("Y-m-d")
TO
$today = date("Y-m-d H:i:s");
with the info you give it's the only thing that comes to mind
The problem with this statement did lie with the way I was using $today but not in the way described above. The problem was solved with a change to the MySQL statement that inserts the data into the database:
This:
$sql="INSERT INTO$tableB($fields) SELECT * FROM $tableA WHERE date_Added='$today'";
Needed to be changed to this:
$sql="INSERT INTO$tableB($fields) SELECT * FROM $table WHERE date_Added between '$date_24_hours' AND '$today'";
Where $date_24_hours is this: $date_24_hours = date('Y-m-d', strtotime($today . ' -1 day'));
There is a need for a comparison between the two dates here otherwise the cron job gets confused with itself and feels it has completed after the first insert completes. Just thought I would share the knowledge since my problem has been solved.
Im trying to avoid reworking a bunch of code. Keep in mind im using the yii framework. I have a table in mysql database that has one field name datetime is the type is datetime. So it holds something like this.
2014-01-31 09:20:00
I need to query that table with only the date along to find all records for that date.
$date = 2014-01-31
$app = Appointments::model()->findAllByAttributes(array('datetime'=>$date));
My problem is that it will not return anything because the time is also included in the datetime record. Is there a way for me to either add a universal time that mysql will read and select anytime or can I do some kind of query that will just ignore the time and just select the dates?
You can use a condition datetime BETWEEN '2014-01-31 00:00' AND '2014-01-31 23:59'. (By the way, consider a better name for that column.)
But you cannot express that with findAllByAttributes, you will need something like this:
$date = '2014-01-31'; // the date
$start_time = $date . ' 00:00:00';
$end_time = $date . ' 23:59:59';
$apps = Appointments::model()
->findAll('datetime BETWEEN :start_time AND :end_time', array(
':start_time' => $start_time,
':end_time' => $end_time,
));
Edit: I see other answers recommending a different approach - asking MySQL to convert its stored value to match your format. That does not scale! If you have thousands of records, this does not make a significant difference, but if you get a few orders of magnitude more than that, you'll see nasty delays.
If you just ask MySQL to match a value without converting it like I suggested, you can put an index on this column and get answers very quickly.
If you ask MySQL to convert the stored values, it has to convert each record in the table to determine if it's a match. Other databases can put an index on the converted value, but MySQL cannot, therefore each time this query is run, it has to convert each record again...
$sql = ".........where date_format(datetime,'%Y-%m-%d')={$date}";
$app = Appointments::model()->findAllBySql($sql );
I am trying to create a date and add 3 days to it, I am then saving it into a mysql db with the date field type.
Here is what I have.
date('Y-m-d', strtotime("+3 days"));
Then I insert into the db. I end up getting 0000-00-00 instead of the date, but if I echo it out it looks correct.
However, if I use
date('Y-m-d')
then insert it into the db, it works fine.
Any help would be greatly appreciated!
EDIT
I found out doing more debugging that my value isn't being passed to my query, I didn't catch this before as I am using a built in CMS function and it wasn't thowing me an error.
Things to check:
output of date('Y-m-d' and date('Y-m-d', strtotime('+3 days')) (echo them out).
show the query string you're inserting this value into, including the file version of the query before you execute it
Consider to switching over to using pure MySQL operations for this, .e.g.
INSERT INTO ... VALUES (now() + INTERVAL 3 DAY)
instead, saving a few roundtrips from native->string->native->etc...
I am trying to compare two sets of times in order to find out if they're overlapping. Here is what I have at the moment..
$sql = "SELECT * FROM schedule WHERE starttime>='$starttime' AND endtime<='$endtime' AND day='$updateday'";
Now this doesn't work as it appears you cant compare time values...so I am completely unsure how this can be done?
Datetime fields in MySQL are stored as (for example)
'2011-05-03 17:01:00'
so you should be able to do something like
$starttime = date('Y-m-d H:i:s', $timestamp);
where $timestamp is a timestamp of the time you are concerned about. Then continue with your query.
You can make timestamps by using mktime() or strtotime() (if starting from a string representation of a time, like from an earlier MySQL query), or just time() for the current time.
I understand that you are using "time" for your datatype. This shouldn't be a problem, since you CAN compare fields using the "time" type. You might want to set your error reporting level to maximum or output your $sql statment just before mysql_query to doublecheck that you are constructing query which can return results at first place. Also check that you have valid dataset for your query in database (has happend to me once while debugging).
Why don't you use use unix timestamp to compare?
$sql = "SELECT * FROM schedule WHERE UNIX_TIMESTAMP(starttime)>=$starttime AND UNIX_TIMESTAMP(endtime)<=$endtime AND day='$updateday'";
Also I think you're comparing strings, which I'm not sure if it would work.
Assuming that you're using the TIME type, your format of "10:00:00" should work.
Not to sound like your mother, but be sure to parameterize your query after you get it working.
I did run the SELECT statement on the phpmyadmin on the sql before trying to do it on the webpage and was great, the thing is to use it and be thinking in this before:
CAST('the_value_you_want_to_be_compared', AS the_type_you_want_to_compare)
example:
If you want to compare the date of a day and your table name is activities:
SELECT * FROM activities WHERE date = CAST('2015-10-29', AS date)
and so on...
I am using DATETIME as a column type and using NOW() to insert. When it prints out, it is three hours behind. What can I do so it works three hours ahead to EST time?
I am using php date to format the DATETIME on my page.
If the date stored in your database by using NOW() is incorrect, then you need to change your MySQL server settings to the correct timezone. If it's only incorrect once you print it, you need to modify your php script to use the correct timezone.
Edit:
Refer to W3schools' convenient php date overview for information on how to format the date using date().
Edit 2:
Either you get GoDaddy to change the setting (doubtful), or you add 3 hours when you insert into the table. Refer to the MySQL date add function to modify your date when you set it in the table. Something like date_add(now(), interval 3 hour) should work.
Your exact problem is described here.
Give gmdate() and gmmktime() a look. I find timestamp arithmetic much easier if you use GMT, especially if your code runs on multiple machines, or modifying MySQL server settings isn't an option, or you end up dealing with different timezones, day light savings, etc.
I would suggest inserting the date in UTC time zone. This will save you a lot of headache in the future (Daylight saving problems etc...)
"INSERT INTO abc_table (registrationtime) VALUES (UTC_TIMESTAMP())"
When I query my data I use the following PHP script
<? while($row = mysql_fetch_array($registration)){
$dt_obj = new DateTime($row['message_sent_timestamp']." UTC");
$dt_obj->setTimezone(new DateTimeZone('Europe/Istanbul'));
echo $formatted_date_long=date_format($dt_obj, 'Y-m-d H:i:s'); } ?>
You can replace the datetimezone value with one of the available php timezones here: