mySQL time sum php now() in laravel seed - php

I have an expires_at column in mysql table. I want to fill this table using Laravel Seeding. Also i have "life_time" column in another table.
For example;
if the "life_time" is 00:05:00, my expires_at should be NOW() + 00:05:00...
expires_at is timestamp but life_time is only time. How can i create an expires_at using Carbon::now() + life_time or any time method which is showing current date.
Actually, i am trying to "A user send an image to other user using my cloud. This image has an expire date which is, if the other user does not download image from my cloud in life_time, i will delete." ... So i created a life time each file types. 5 min for images, 10 min for videos etc. I know send date and life time so i need to create expire date.

The simplest solution is to keep life_time in minutes like most of systems do and use Carbon to create expires_at:
Carbon::now()->addMinutes($lifeTime)

There are many in-built functions in Carbon. you need to use addHours(),addMinutes() , addSeconds() to add specific time in exist time.
For more information please visit : http://carbon.nesbot.com/docs/
Hope this will help you.

Related

Php/MySQL Auto date checking/comparing

Wanted to ask You how can I setup something on my php website, that would everyday automatically check and compare current date to all the database datetime entries and delete the rows of the dates that are in the past (for ex. if the current date is 2014-03-17, it would delete the rows that have datetime of 2014-03-16 ).
Because I basically have a TV-package website (not a real thing, just for a class), where you can order a package, you enter for how long and it adds that amount to current date, writes the order into database with the date written into a field named "expires". Would it make sense if I just wrote the checking function into the index, so when someone visits the site it would delete it? If so, how could I compare the two dates?
The DB example looks something like this: http://s29.postimg.org/7sbgj2hnr/dbtest.png
Although I highly recommend a scheduled task, you can do it in PHP by calling:
$sql = "DELETE FROM tableName WHERE `expires`<'".date('Y-m-d')."'";
Convert the date to a unix timestamp and compare it against the value of time() like you would any other integers.

Calculate the difference between two timefields

Hi i am using third party API to display the feeds in our site, in that i have fields called opentime and closetime in db table. The feed table is being updated for every 20 seconds .Things working fine, now the problem is with opentime and closetime. Its giving 3 Hrs time in the future(it might be their server time) from the current time in my server. Say example, if my server current time is 8:00 AM, at the time feed table is being updated the opentime as 11:00 AM(3 Hrs from now, sometime its varying). We can't do any changes with feed table since the table is updating automatically through API call.
So i want to find out the time difference between these two(opentime - currenttime), how can i able to convert the opentime into my server time.
I tried the following code in my-sql query,
TIMESTAMPADD(MINUTE,-180,FROM_UNIXTIME(opendate) // subtracting 3 hours(180 min) from the opendate.
Is there any other way to do this?
Advance thanks for your guidance.
You can change the default time zone for MySQL. This can be set as a default for your MySQL server, or on the fly with each connection. Something like this:
set timezone='your timezone';
http://dev.mysql.com/doc/refman/5.5/en//time-zone-support.html
Set timezone for mysql config
set timezone='your timezone';
Try it
SELECT TIMESTAMPDIFF(MINUTE,'2012-10-21', NOW()) as diff;
diff=1997

MySQL - Single DATETIME or Separate DATE and TIME Columns?

In my application I'm developing a functionality for creating "reminders".
A reminder has a date and a time. In my application, I have a form to create / edit reminders - this has two separate fields to input this information:
<input type="text" name="date"></input> <!-- datepicker plugin -->
<input type="text" name="time"></input> <!-- timepicker plugin -->
Now as a rule I have always used a DATETIME column whenever I have needed to store date/time, however this is the first time I'm having to store a user inputted date/time.
I figured it would be best to have seperate DATE and TIME columns, because it would be easier to insert / retrieve the data to / from my application. For example I won't have to combine the values from the two input fields to create a single value to insert in to the database. And likewise I won't have to split a single value in to two values to populate the form fields in edit mode.
But on the other hand won't it be easier to query the table if I used one column? What do you think?
You should build bottom-up (database at the bottom). Don't think about the application, just the database. Now, what makes sense at the database level. DateTime.
So you need to write extra code at the application level.
Please see it
Adding a Timepicker to jQuery UI Datepicker
http://trentrichardson.com/examples/timepicker/
convert your date time according to your mysql format and store it
$mydate = strtotime($_POST['date']);
$myfinaldate = date("d-m-y", $mydate);
$mytime = strtotime($_POST['time']);
$myfinaltime = date("H:i:s", $mytime);
Seperating columns is unlogical. You can use timestamp as datatype and you can use mktime function to parse date and time easily.
Doesn't it depends on the system you're creating.
If you want to store dates beyond 2038 I would store the datetime and time separate.
what if you are developing a reservation application and at one end you need to know on what date and at what time to schedule an appointment for a user, and at the other end, you need to match the user to a doctors schedule. You save the doctors schedule in a database and you need to know (amoung other things) when the doctor is available (on what days), and at what times. Let us forget about the on what days for a moment, and focus on the time shedule first...
You need to develop a programmable schedule so that if you know that the doctor works 6 months in a particular calendar year. (Jan - Jun), He or she may work (9-5 M,W,Fr), and (10-3 T,Th). Sat and Sunday the doctor is off. So you develop a table to hold the Daily time schedule with 2 columns to hold the daily starttime and daily end time for each day of the week. 14 columns in total and a primary and possibly secondary key. So now its time for some date arithmetic (This is where it gets hairy:-|...
You can say i your query: (mySQL)
Select such and such...
where form.theapptdatetime between doctorschedule_startime_tuesday and doctorschedule_endime_tuesday
and this will do a match to see if your datetime is within the date range of your doctorschedulestartime and endtime... but what if all you need is the time??
will the date arithmetic still work if the time value is stored as a datetime???
In other words if I have 01:00:00 as my doctorschedule_startime, is this a legitimate date value for my arithmetic to work, or will a date portion be forced upon me.
Perhaps I should store the time as a varchar, and convert it to a suitable datetime value and perform the arithmetic in the code instead of the query????
An example comes to my mind as to when have date and time split:
You could want to have DATE a part of the unique index, so that a user is only allowed to add 1 record to some table per date, but still you want to know the TIME he added it, so you keep DATE and TIME separate.

Adding values to a Timestamp

I am trying to create a script for quiz. I have stored current timestamp in MySQL DB as quiz start time, and want to check after each quiz how much time is left.
I have the idea that I will add 30 mins to saved time stamp and subtract this value from current time. That will be the time left. But I don't know the exact way of doing this.
My time stamp is saved in DB in format 2010-08-24 20:08:59. Any one have the idea.
Please let me know if someone have done it, or know how to get it.
Adding 30 mins to time stamp and showing the user how much time is left.
I am using the now() function to store the timestamp in DB.
Thanks
I would personally store the output of PHP time() in the database.
If you a human readable format from this value, you could use date('Y-m-d H:i:s', $fromdatabase);.
You want to store an actual UNIX timestamp in the database, not a string in that format.
You may or may not be doing this already, it depends on the type of column you're using. For MySQL, you should be using TIMESTAMP, which allows you to retrieve the timestamp with
SELECT UNIX_TIMESTAMP(column_name) ...
To store the current time + 30 minutes, all you have to do is:
INSERT INTO table (column_name) VALUES(UNIX_TIMESTAMP() + 1800)
You can know if the time has expired by comparing time() against the value of the column.

I want to hide the details of record (of database) from user when date will be expired using php

I'm totally new to php & mysql,
When I'm creating one trial application using php ,there is problem specify below,
I want to hide the details of record (of database) from user when date will be expired using php ,
When one user logedin & create one record entry that time automatically current date is also entered in database, but I want to give 1 week expiry date, after that the record will be not shown to the front end but it available in database. When the creator of this record renew this recod that time automatically date should be updated at current date, & same record will be shown as new entry
PLz help me,
I'm waiting for ur answer,
Thanks in advance.
What kind of column is the date one? Timestamp? or timedate?
Either way, you can do it like so (depending on your column type)
For timedate:
WHERE (date + INTERVAL 1 WEEK) < CURDATE()
For timestamp:
WHERE (timestamp + 604800) < UNIX_TIMESTAMP()
Let me know what you are using for your date column and I can update. You can also see the MySQL date functions here.

Categories