How to Set an Expiration Date after a Year in Laravel - php

I'm trying to make an investment website where I want to set an expiration date exactly after a year that the registration was made. Upon the registration, an initial deposit is required which is the one with the value "500" which is fixed on every registration. I want to use the created_at timestamp from my database table money_trade_deposits as the reference for the start date and calculate the expiration date based on that after a year and pass it on my laravel blade view. I'm new to laravel and I have no idea how to this.
This is my money_trade_deposits table and I wanna base the expiration date from the created_at date.
Or it it possible to add a new timestamp row which will automatically calculate the expiration date adter a year? Any help will be much appreciated. Thanks a lot!

Since the created_at is automatically casted as a Carbon instance in Laravel, you can calculate the expiration date one year after with the ->addYear() method.
Ex: $model->created_at->addYear()->toDateTimeString().
Reference: Carbon doc

You must use Carbon here to add a year from the day of the initial deposit.
Laravel automatically wraps up created_at to Carbon instance, and Carbon instance you can easily add year using addYear() function. See Documentation https://carbon.nesbot.com/docs.
I will suggest you to create a new column for expiration date and set it initially rather than adding a year to created_at each time to check expiration. Anyway, Carbon is very useful for this type of date processings.

Related

mySQL time sum php now() in laravel seed

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.

Carbon - Check one month to go, one week to go etc

I have a list of members on my website, and I have their membership start date and expiry date stored in my database as timestamps. Now I want to send them emails
one month before membership expiry date
one week before membership expiry date
one day before membership expiry date
How can I determine if there is one month/week/day is left in expiry based on stored expiry date?
The project is done in Laravel 4.2, and I am using Carbon.
Any ideas?
See Carbon for all the details.
$now = Carbon::now();
$week = Carbon::now()->addWeek();
$month=Carbon::now()->addMonth();
if expiry_date is the stored expiry date and Member is the model, using eloquent, this will give all the members expiring in one week.
$members=Member::where("expiry_date","<", $week)->get();

How to update cache once a day on the day (00:00:00) in Laravel 5?

I have a data set that has to be updated only once every 24 hours. This should happen with the first request of the day at or after 00:00:00. how do we set the cache remember condition for this as it only allows for setting minutes not the time?
You can use Carbon for that. In the Laravel documentation we can find an example to expire a cache after 10 minutes:
$expiresAt = Carbon::now()->addMinutes(10);
Cache::put('key', 'value', $expiresAt);
All you want it to determine the end of the day. Luckily Carbon offers everything:
$expiresAt = Carbon::now()->endOfDay();
This will give you the last second of the current day 23:59:59. Just add one second to satisfy your requirement:
$expiresAt = Carbon::now()->endOfDay()->addSecond();
Not the complete answer, I was trying to find a way to cache at start of day OR first request of day. Going by append date to predefined key
So what you want is simple, if you want a simple solution you can add a table in your DB and it simply has two attributes: id and date. From here when someone login you check if there was a login that day, if not you do what you have to do.
EDIT
well when someone logs-in you update the date in the table.

Subscriptions and MySQL database structure

Just a quickie,
I have a date in my database where a subscription to a service starts...
In your experience, do you think it's better to record the expiration period as a time.. so then i can add the time onto the date_created to see if it has expired... Or shall I just workout the date it will end in php and put that into a datetime in the database?
So, TIME or DATETIME for subscription length.
Ask me if you want more details.
I would use the end date, this way you only need to make one calculation when you add the record to your database. Every other time you can just compare the current date to the expiration date.
I'd be tempted to save the start date, as if you're storing the expiration date only and you want to change the subscription length from say, one month to three months, you've got to try and work out the original start date and then add your new subscription length back on.

validate time field entry

is there any easy to validate a time field entry against the current date and time?i get date and time entry from an user in two separate fields. I used jquery date picker and time picker for both. with the date I had the option to show current date and future dates and not the past dates so it's good. with the time field i have to show all the time but want to somehow validate to see if the time is already gone for that date.I can o away from jquery and just use php if possible. any ideas?g
You can do
$ispast = strotime($field_value) < time();
This will tell if the given time is in the past. The only requirement is that $field_value is in a format that strtotime recognizes. This will interpret the date in the default timezone you have set (see date_default_timezone_set).

Categories