validating time and date fields - php

Is there any way to validate the time field based on the current time. I already posted a simillar question but now i decided to move away from that option and just want to try this.
1) ask an user to select time (I am using jquery timepicker).
2) See if that time is future time and not which is already gone.
I also have a datepicker, so if there is a way to check the time for that date then that would be great. For example, if one user select "august 10 2010(todays date)" from the date picker and then select time using timepicker, let say user select 6am , is there any way to see if that time for that date is already gone? because that user cannot choose a time which is already gone. I am fine if there is any php solution to take these two fields and see if it's a valid entry or not. Please guide me.
I just found that the date picker for the current and future date is working based on system date, I had my system date changed to aug 6th and the date picker started showing from Aug 6th. Any idea if this can be fixed?

You can use mktime() to solve all of those problems. It will create a unix timestamp if the date is correct (otherwise it will return false). And you can also check if the value is greater than the current timestamp (using the time() function).

Related

Laravel update database if date matches today

The user is able to enter the amount of rain collected each day, by entering the amount of rain collected, and date. They should be able to enter rain collected in the past as well (by giving another date if needed).
So, how can i check (by using the created_at row) if the user has already entered some data for the current date? And if they have, update the value given for the specific date. I already know how to update etc, I just need a way to validate the date given.
I was trying to figure something out by using Carbon, But my head is about to explode, I can't seem to wrap my mind around this issue.
'created_at','>=',Carbon::today())
I know that wont work. The created_at looks like this:
2014-07-16 20:42:38
So I would need a way to check the current date, and skip the time/clock? How would my approach be on this?
Create your condition in query like below, to match only date in yyyy-mm-dd, to check the current date.
->whereDate('created_at','=',Carbon::today()->format("Y-m-d") )
Carbon::now()->toDateTimeString();
See how you can work with dates

How to add 1 hour to a queried time from the database?

I'm a beginner for php and developing this web application, which users who registered on this site, can be claimed some scores in every one hour. When a user claims at some time, database stores that time as time data type in to user_claim_time column. When that same user tries for his next claim, this php script be able to get his last claim time and add one hour to check if the user really claims in an one hour.
So, my question is how can we add one hour to queried time. I'm using php time(h:i:s) function to store server's current time into the database.
You can do something like this:
SELECT * FROM your_table
WHERE user_claim_time < NOW() - INTERVAL 1 HOUR
However i recommend you to use user_claim_time column in datetime format.
Because time like this '00:00:00' will produce negative output as one hour subtraction can change the date or month as well. For example date like this '2017-08-01 00:00:00'.
So using datetime is the right way i think to properly compare time difference.

Retrieved date, time off by 7 hours?

I am having trouble displaying the correct date, well actually its only the time.
In the db the date is stored in this form: '2012-09-28 23:30:00' (off by 7 hours of actual date time)
That is the value for a date of an event.
When I retrieve it using this script(for drupal):
$ttdate = $obj->field_date_test_value;
$nttdate = strtotime($ttdate);
$okdate = format_date($nttdate, $type = 'medium');
print $okdate;
...it works fine but the time is off by 7 hours. So instead of showing 'Fri, 9/28/2012 - 4:30pm' it shows 'Fri, 9/28/2012 - 11:30pm'.
Note that on the event's page, drupal retrieves the correct time...
I did some research and I figured it has to do with the timezone? But I have the timezone set to Los Angeles so I am not quite sure what is going on. Maybe its my script?
I assume that you use the Date module.
This is probably because the Date module saves the date in UTC and alters the output on rendering (using format_date(), I think). Since you do not format the date in your own code, you see the raw UTC time (which is PDT + 7 hours) even though the correct date is displayed by the event page.
The bottom of this page explains how timezones are handled in the Date module. My guess is that either the first or third scenario is relevant in your case.
Try the following in mysql:
set time_zone = '-07:00';

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).

What I have minus months in database (mysql), is it the php code?

Description:
I am doing purchasing function, whenever the users make purchases, it will add 1 month privilege to my access special page of my site. I have received some complaints from my users, please read the problem section.
Problem:
Out of 500 users, there are few users, that for example: make purchase today, but the expiration day goes backward, 1 or 2 days, or even months. For example: He purchased on 3 August 2010, the expiration date is 25 May 2010.
My Php code:
The code that I am using to add 1 month before insert into mysql database is:
// I already set the default timezone
$expiryDate = date("Y-m-d H:i:s",strtotime("+1 month"));
I am not sure if its the code is wrong, my server is wrong or the third-party payment gateway is wrong, please advise me how to solve it.
!important question!
What are the possible causes, that can cause the expiration date goes backward?
If you're storing the expiry time in the database as a date or datetime field, then you should do the update there:
UPDATE table SET expiry=DATE_ADD(expiry, INTERVAL 1 MONTH) WHERE ...;
Full details here.
There are issues using +1 month with strtotime, there are some solutions on the function document page. However if i were you i would use a tested library like Zend_Date. You could use the sql as suggested but im going to guess thats going to depart from how you organized your DB specific code.

Categories