How do I know in PHP that the date entered by the user in future than the current date.
i.e
if($current_date<$future_date)
{
//do all the success actions here
}
else
{
//show the user that they have selected a date in the past.
}
Well you first need to convert the date string using strtotime() function and then check if future date value is greater than current date value.
Here is the code snippet:
$today_date=date('Y-m-d H:i:s');
$current_date=strtotime($today_date);
$future_date=strtotime($future_date);//retrieved from user's input
Now you can call your function:
if($current_date<$future_date)
{
//do all the success actions here
}
else
{
//show the user that they have selected a date in the past.
}
I suppose you know the date format in which your users enters their date cos it will help you understand what date format you will be working with. But irrespective of whatever date format you are working with, you can always convert it to a more suitable one using:
strtotime()
Below is an example of checking a future date (working strictly with date)
$currentDate = date("Y-m-d");
$userFututeDate = "2014-05-08";
if($userFututeDate > $currentDate){
echo 'You have selected a date in the future';
}
else{
echo 'You have selected a date in the past.';
}
Below is another example of checking a future date (working with date and time)
$currentDateTime = date("Y-m-d H:i:s");
$userFututeDateTime = "2014-05-07 05:05:08";
if($userFututeDateTime > $currentDateTime){
echo 'You have selected a date in the future';
}
else{
echo 'You have selected a date in the past.';
}
Perhaps, you need to understand how to play around with strtotime(), find some sample below:
$dateVar = "25-07-2014 05:05:08";
echo date("F j Y", strtotime($dateVar));
echo '<br><br>';
echo date("Y-m-d", strtotime($dateVar));
echo '<br><br>';
echo date("F jS Y, g:i a", strtotime($dateVar));
Check out for more on strtotime()
Related
i have the expiry date saved for each post using date time picker, now i need to check it against the current date and time each time aperson visits the page and display msg according to whether its expired ornot...but the comparison fails
i tried with date(),date create from format and DateTime::createFromFormat...but it fails the check if (is_a($expirydate, 'DateTime')).
$todaydate=new DateTime();
$todaydate->format('d/m/Y H:i:s');
$expirydate=strtotime(get_field('expirydate',$post->ID));
$expirydate=date('d/m/Y H:i:s', $expirydate);
if (is_a($todaydate, 'DateTime')) {
echo "today date is datetime";
if (is_a($expirydate, 'DateTime')) {
echo "expiry date check passed";
}}
if ($expirydate >= $todaydate)
{
echo "not expired";
}
it echos the msg today date is datetime but thats all...doesnt display msg "expiry date check passed" or "not expired"...its shud diplay all of the 3 messages.
Work with strtotime is Old School. Better use DateTime. DateTime objects can also be compared directly.
$expirydate=date_create(get_field('expirydate',$post->ID));
//$expirydate=date_create('today 11:19'); //for a test
$todaydate=new DateTime('Now'); //with current Time
//$todaydate=new DateTime('Today'); //with Time 00:00
if($todaydate > $expirydate) {
echo 'expired';
}
I have the following function which works well but would like to check the returned date and compare with the current date if before current date to show something if current or in future show as normal.
Function:
function dateFormat( $old, $correction ) {
$old_date_timestamp = strtotime( $old );
$new_date = date( 'jS F Y', $old_date_timestamp + $correction );
return $new_date;
}
Call:
echo '<li class="list-group-item">Support Expires: ' . dateFormat($purchase_data['verify-purchase']['supported_until'], 11*60*60 . '</li>');
Output:
2nd March 2016
So as not today's date and/or before today's date would like to echo a message, else just show the date.
In PHP it is very simple to compare two different dates using < = > like you normally compare numbers. The only step prior to this is below:
//Tell PHP that the value in variable is a date value
$date_1 = date_create("2017-05-29"); //This value can be any valid date format
date_1_formatted = date_format($date_1, "Y-m-d"); //This formats the date_1
//Now you can simply put the second date, for example, today.
$date_2 = date_create("2017-04-29"); //This value can be any valid date format
date_2_formatted = date_format($date_2, "Y-m-d"); //This formats the date_1
//For current date, it is simpler
$date_today_formatted = date("Y-m-d");
//Now you can compare these two dates easily
if ($date_1 < $date_today_formatted) {
echo "Date 1 falls before today.";
}
else {
echo "Date 1 falls after today.";
}
Hope this helps!
I managed to work it out using the following 2 functions:
function dateFormat( $old, $correction ) {
$old_date_timestamp = strtotime( $old );
$new_date = date( 'jS F Y', $old_date_timestamp + $correction );
return $new_date;
}
function checkLicenceSupport($licence_date) {
$date_now = new dateTime();
$date_set = dateFormat($licence_date, 11*60*60);
if ($date_now > $date_set) {
return 'date expired';
} else {
return 'date valied';
}
}
I have the following function which works well, but would like to
check the returned date and compare with the current date.
If it is before the current date, show something.
If it is the current date, or in future, show as normal.
I needed to rewrite your question, because lack of grammar and punctuation made it confusing. No offense intended.
Your call code has the closing parenthesis for your function call is placed wrongly.
dateFormat($purchase_data['verify-purchase']['supported_until'], 11*60*60)
It is more readable to use full days or hours (in seconds):
11*86400 //(11 Days);
11*3600 //(11 Hours);
The function and code, as you have it now, will always return a date in the future of the date you've submitted via the call. (I can't tell from your question whether this was intended or not).
Currently, there is no "comparison" in your function. But your question indicates you want to compare the submitted date to the current date and then do something in certain cases.
If you are going to use a Unix timestamp, then there's no need for multiple formatting, compare the two dates in Unix, then format the result.
function dateCompare($submittedDate){
//This is only needed if your submitted date is not a unix timestamp already
$submittedDate = strtotime($submittedDate);
$currentDate = time(); // Creates timestamp of current datetime
if($submittedDate < $currentDate) {
//show something i.e. return "Support Has Expired";
}else {
return date('jS F Y', $submittedDate);
}
}
echo '<li class="list-group-item">Support Expires: '.dateCompare($purchase_data['verify-purchase']['supported_until']).'</li>';
What I'm trying to do is insert credit card info from a form to my mysql database.
First of all,
the input format for the expire date will be MM / YY(with or without spaces) on the form and i'm trying to concat the "20" string to the year but I always get "20 YY" as a result, which cannot be recognised from mysql as a date.
I want to get the expire date to fit a format so later on it can be altered into a MySQL format. Any ideas?
This is my code so far:
<?php
$cc_number="";
$cc_expire="";
$cc_cvc="";
$cc_number=$_POST['number'];
$cc_expire=$_POST['expiry'];
$cc_cvc=$_POST['cvc'];
$cc_pid=$_SESSION['pid'];
/*edit the expire date*/
$pieces=explode("/", $cc_expire);
$expire_dd="01";
$expire_mm=$pieces[0];
$expire_yy="20".$pieces[1];
$expire_yy=trim($expire_yy, "\x00..\x1F");
//$cc_expire = $expire_yy.$expire_mm.$expire_dd;
//$cc_expire = date('Y-m-d', strtotime(str_replace('-', '/', $cc_expire)));
echo "expire_yy = ".$expire_yy;
//echo "cc_expire = ".$cc_expire;
if (isset($_POST["submit"])) {
if (empty($cc_number) || empty($cc_cvc) || empty($cc_expire)) {
echo "<p align='center'><font color='red'><br><br><br><br>All fields are mandatory.</font></p>";
}
else {
$cc_expire = date('Y-m-d', strtotime($cc_expire));
$sql = "INSERT INTO credit_card (credit_card_number, cvc, expiration_date, pid)
VALUES ('$cc_number', '$cc_cvc', '$cc_expire', $cc_pid)";
if ($dbconn->query($sql)) {
//echo "<script> window.location.assign('../card.php'); </script>";
}
else {
echo "<p align='center'><font color='red'><br><br><br><br>Something went wrong.</font></p>";
}
}
}
?>
I want for all the cards to get a mySql DATE format which will be 01-MM-YYYY.
strtotime() should work for you. learn more here http://www.w3schools.com/php/func_date_strtotime.asp
Depending on how the data looks like in $cc_expire you may need to parse it first so this first:
$cc_expire = "28-".str_replace(" ","-",cc_expire);
Which should give dd-mm-yyyy format (I've had a issue using '/' before when changing dateformat)
Then do:
$mysqldate = date("Y-m-d H:i:s", strtotime($cc_expire));
This should convert the cc_expire to mysql format ready for insertion.
I am working on a php app and need some help with comparing a date.
I have a date select input field (datepicker) which thanks to client side code will always post a date in the format: mm/dd/YYYY eg 02/25/2015.
What Iam trying to do is assertain that this date is no later than the current date, using php.
Initially I have set the local timezone with:
date_default_timezone_set('Europe/London');
And within the code I have:
} elseif (date(strtotime($_POST['datepicker'])) > date('m,d,Y')){
$displayblock.= "<br><p>The selected date is in the future!!! </p>".date("d/m/Y", strtotime($_POST['datepicker']));
$alertbox = "<script>alert('".$_POST['datepicker']." is in the future!!! Shall we try that again? :-)');</script>";
This is obviously far from graceful and does not appear to be comaparing dates correctly either.
Can anyone help pls?
Many Thanks,
You can also compare them if they are coming as a string with this function. The first date is your date coming from your html/javascript datepicker, the second date is the actual date of server:
function stringDateIsAPastDate($datestring1){
if ((date("j-m-Y", strtotime($datestring1))) <= (date("j-m-Y"))){
//here the code to do when the date inserted from the website is a past date or today
return(true);
}else{
return(false);
}
}
$now = new DateTime();
$now->format('Y-m-d');
$ding = new DateTime($_POST['datepicker']);
$ding->format('Y-m-d');
if($now < $ding){
echo 'datepicker is after current time';
} else {
echo 'datepicker is before current time';
}
Compare Two Dates
$date1 = new DateTime('May 13th, 1986');
$date2 = new DateTime('October 28th, 1989');
$difference = $date1->diff($date2);
Check this :
http://www.paulund.co.uk/datetime-php
And php.net Manual:
http://php.net/manual/en/class.datetime.php
I am using the following code to attempt to compare the current date with a date entry in a mySql database. It's code that I have found online and adapted as all the examples I have found hard-code the date to compare the current date with.
The trouble is even dates in the future are being marked as expired and I can't understand why this would be.
I am afraid that I am still new to PHP, so I may be making a schoolboy error!
$exp_date = KT_formatDate($row_issue_whatson1['dateToShow']);
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date > $today) { echo "Not expired"; } else { echo "expired"; }
Any help would be most appreciated.
I should add that the date time format used in the database entries is dd/mm/yyyy
Instead of making a string then converting it to a timestamp, simply use mktime:
<?php
$today = mktime(
0, // hour
0, // minute
0 // seconds
);
?>
The rest of the values will be filled according to today's date. If this still gives problems, put in some echo's for the values of $exp_date and $expiration_date.
Edit
Since this solved the problem, the discrepancy you were seeing was because you were doing the opposite with date('d-m-Y'). You were asking for the current date and the time values are then filled in with the current time. The expiration date in the database is likely set at midnight. With both dates being equal, and it being say 11am now, you are comparing if (00:00:00 > 11:00:00) which fails.
$exp_date = 14/05/2011 // todays date, int
$server_date = server.date() // servers date, int
// check exp_date against server date
if ( $server > $exp_date)
{ echo "Sorry your 'service' has expired"; }
else
{ echo "Welcome 'members_name' to StackOverflow"; }
Try that. However you need the right date format, as server.date() is probably different in PHP.
If problem still persists I would check whether your dates are strings or integers or both. That could possibly be the issue.
Hope that helps.
DL.
Your function does not seem to be valid.
function KT_formatDate( $exp_date){
$exp_date = strtotime($exp_date);
$now = time();
if ($now > $exp_date)
return 'expired';
else
return ' Not expired';
}
$response = KT_formatDate($row_issue_whatson1['dateToShow']);