Help implementing past date validation - php

I need to implement something that checks if a given date is greater than today. If for example, I input a date of April 19, 2011 while today is April 15, 2011, there should be some validator/pop up error. How do I implement this?
I have my system date (today's date) working fine through php. I just don't know how to create a validation/error message when the user inputs a higher date than today.

It can be done with PHP (on server side) and with JavaScript (on client side, in browser).
Here is an example of how to do it on JavaScript:
var currentTime = new Date()
month = currentTime.getMonth(),
day = currentTime.getDate(),
year = currentTime.getFullYear(),
today = year + "-" + month + "-" + day;
var users_day = '2011-04-19';
if (users_day > today) {
alert ("Entered day is greater than today");
}
else {
alert ("Today is greater than entered day");
}

For example (in PHP)
date_default_timezone_set(date_default_timezone_get()); // not necessary here
$today = strtotime('2011-04-15');
$users_day = strtotime('2011-04-19');
if ($users_day > $today) {
echo "Error";
}
else {
echo "OK";
}
Example above outputs
Error
...because April 19, 2011 (user's input) is greater than April 15th, 2011 (today).

Related

How to check if date of year has passed using Carbon/Laravel?

In Laravel, I'm trying to check if the current date is before or after a specific date in any year. A client's fiscal year ends October 31st and I'm having trouble checking if today is before or after that.
I've tried using the built in comparison functions in Carbon but I can't figure out what to compare now() with, since all Carbon instances seems to include a specific year. How do I check if now() has passed that day in any calender year?
The code below works but using dayOfYear() will return different values on leap years and seems like a poor solution.
if (now()->dayOfYear >= 304) {
$current_year = now()->year;
} else {
$current_year = now()->year - 1;
}
How can I return true if now() is after October 31st in any year?
It looks like you already know about using getters with the Carbon object to find pieces of the date. If you're looking to see if the current date is after October 31, just use them some more:
if (now()->month > 10) {
$current_year = now()->year;
} else {
$current_year = now()->subYear()->year;
}
To check if the current date is before any given date
$isBefore31October = now()->lt(Carbon::parse('2020-10-31'));
//Will return false if now() occurs after 31st October 2020
Similarly to check if the current date occurs after a given date
$isAfter31October = now()->gt(Carbon::parse('2020-10-31'));
//Will return true if now() occurs after 31st October 2020
For setting the current year depending upon whether current date occurs after 31st October of current year
$fyEnd = Carbon::parse(now()->year . "-10-31");
$current_year = now()->gt($fyEnd) ? now()->year : now()->year - 1;

How to validate a date in PHP when the format can vary

I am working on a site and have to validate a date from a form. i am using jquery picker to select the date, however, the user can change the selection manually therefore i want to validate for any possible format (January, 1 2000, 1 jan 2000 etc).
I have looked around and am having trouble finding a solution. I am trying using strtottime to turn the date to a stamp and I am then using checkdate. The validation with strtotime works fine if i enter for instance January 40, 2000, however, if i enter a date that is below or equal to 31 with any month strtotime moves it to the following month, for instance, february 31, 2017 turns to March 3, 2017.
This is making the checkdate function useless as the date passed to it is always valid. is there a solution to this?
$thisD = 'February 31, 2017';
if (strtotime($thisD) === false)
{
echo "<BR>DATE FORMAT NOT VALID <BR>";
}
else
{
echo "<BR>DATE FORMAT VALID <BR>";
$thisDStr = strtotime($thisD);
echo "Original date was $thisD, date now is " . date('F, j Y',$thisDStr). ' -> ';
if (checkdate(date('n',$thisDStr), date('j',$thisDStr), date('Y',$thisDStr)))
{
echo ' is valid <br>';
}
else
{
echo ' is NOT valid <br>';
}
}//end else

Detect BST in php

I am building a booking system in php that offers session times for people to book outdoor activities.
In the summer months, there is an extra session available at the end of the day, because of Daylight Savings, there is an extra hour in the evenings.
Year Clocks go forward Clocks go back
2014 30 March 26 October
2015 29 March 25 October
2016 27 March 30 October
2017 27 March 30 October
2018 25 March 28 October
I am using this...
$todaysDate = strtotime(date("Y-m-d"));
$bstBegin = strtotime("2015-03-29");
$bstEnd = strtotime("2015-10-25");
if($todaysDate > $bstBegin && $todaysDate > $bstEnd)
{
echo "<option value="evening">Evening Session</option>";
}
I only need to show this extra option in the select list between these dates. Is this something I will need to set manually from year to year, or is there a PHP date variable that knows the days the clocks change?
$today = strtotime(date("Y-m-d"));
if (date('I', $today)) {
echo "We're in BST!";
} else {
echo "We're not in BST!";
}
or use the DateTime object equivalent, which maintains details of all the transition dates globally
I work normally with the DateTime functions and like it very much. You have a lot of possibilities to modify a date.
But in your case you can concat the actual year to your string.
$todaysDate = strtotime(date("Y-m-d"));
$bstBegin = strtotime(date('Y')."-03-29");
$bstEnd = strtotime(date('Y')."-10-25");
I hope i have understood your problem correctly.
I have found this online very good ref : https://gist.github.com/aromig/56376f76d4fb653ba83e
public function is_BST() {
$theTime = time();
$tz = new DateTimeZone('Europe/London');
$transition = $tz->getTransitions($theTime, $theTime);
$abbr = $transition[0]['abbr'];
return $abbr == 'BST' ? true : false; }

Using jquery countdown timer with mysql datetime?

I know this question has been asked many times as I have found a few on google and also on stackoverflow.
but none of them explained how to format my datetime in my php so it works in combination with jquery countdown timer. so I am going to ask it here in a hope i get someone shed a light on this for me.
Basically what i am trying to do is to create a countdown timer which will work with mysql datetime.
the datetime is stored in mysql so All need to do is to get the correct format in my php so the countdown timer could work with it.
I am using this plugin: http://keith-wood.name/countdown.html
and here is what i have so far:
PHP formatting:
$end_date = date("m d Y H:i:s T", strtotime($row["end_date"]));
Jquery/Javascript code:
<script type="text/javascript">
$(function(){
var countdown = $('#countdown'),
ts = new Date(<?php echo $end_date * 1000; ?>),
finished = true;
if((new Date()) > ts)
{
finished = false;
}
$('#defaultCountdown').countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds)
{
var message = "";
message += days + " days, ";
message += hours + " hours, ";
message += minutes + " minutes, ";
message += seconds + " seconds ";
message = (finished ? "Countdown finished" : "left untill the New Year");
countdown.html(message);
}
});
});
</script>
when i run this code, all i get is 0 hours, 0 minutes, 0 seconds.
I can only suspect that the issue is from formatting the datetime in my php section!
or am i missing something else as well?
okay I have managed to minify the code to this:
<script type="text/javascript">
$(document).ready(function () {
$('#defaultCountdown').countdown({
until: new Date(<?php echo $end_date; ?>),
compact: true
});
});
</script>
and changed the php to this:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
However, the time shown in the coutdown timer is wrong (way off).
the $end_date is: September 22 2013 23:30:00 GMT in mysql datetime
but the jquery countdown timer is showing:
34d 06:21:48
2013, 9, 22, 23, 30, 00
34days and 6 hours blah blah is absolutely wrong!
what am i doing wrong here now?
The JavaScript Date object is constructed as follows:
Date(year, month, day, hours, minutes, seconds, milliseconds)
That means you probably should be doing something along these lines:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
Sources:
JavaScript Date-object
PHP date-function
EDIT:
In addition, I seem to have found the problem in the jQuery Countdown manual:
A note on Date - the JavaScript Date constructor expects the year,
month, and day as parameters. However, the month ranges from 0 to 11.
To make explicit what date is intended (does a month of 3 mean March
or April?) I specify the month from 1 to 12 and manually subtract the
1. Thus the following denotes 25 December, 2010.
So, you'd have to split the string, substract 1 from the month and rebuild...
$tmp_date = explode(', ', $end_date);
$tmp_date[1] = $tmp_date[1] - 1;
$end_date = implode(', ', $tmp_date);
Link to jsFiddle

Check if date is before or after a certain date in the same month

I am developing a web app using PHP (Codeigniter).
When a user makes a request for a certain resource in the app I need to check if todays date is before or after the 25th of this month. How can I do this? I do not know how to do it.
This should do it...
if ( date('j') < 25 ) {
// date before 25th
}
You can use the date function with the j format type for that
if (date('j') > 25) {
// It is after the 25th
} elseif (date('j') < 25) {
// It is before the 25th
} else {
// It is the 25th
}
See http://php.net/manual/en/function.date.php for more info

Categories