match datetime with database datetime - php

I have a exam form. But I want to show that form within given date and time. Means if start date time is 2016-06-24 11:34:04 and end date time is 2016-06-27 00:00:00 If any merson open form between given date and time the form should be open otherwise it will go on another message page. I have a code .But it is not working. Please help.
<?php
include("connection.php");
date_default_timezone_set('Asia/Kolkata');
$timestamp = time();
$date_time = date("Y-m-d H:i:s", $timestamp);
$result=mysql_query("select * from activeform");
while($row=mysql_fetch_array($result))
{
extract($row);
if($date_time > '$startdate' and $date_time < '$enddate')
{
header("Location: myaccount.php");
}
else{
echo"date has been exceeds";
}
}
?>

You can use strtotime if you would like to compare dates in PHP. You may want to look at this answer

Related

Comparing a Date in PHP

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

How to determine if date entered by the user is in future?

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

PHP Date Calculation to display the next 1st June

I need to be able to calculate a date using PHP that displays the next 1st of June. So, today is 15th April 2013 therefore I need to display 01/06/2013 (UK format). If the date was 5th August 2013 I would need to display 01/06/2014.
Can anyone help?
Thanks,
John
You can achieve this using :
$now = time();
$june = strtotime("1st June");
if ($now > $june)
echo date("d/m/Y", strtotime('+1 year', $june));
else
echo date("d/m/Y", $june);
Hope this helps :)
For this you can achieve by checking the present month
if(date('m')>06)
{
$date= date('d-m-Y',strtotime("next year June 1st"));
}
else{
$date= date('d-m-Y',strtotime("this year June 1st"));
}
echo $date;
Create a new DateTime object for the current year. DateTime is the preferred way to handle dates in PHP.
If it's too early, create a new datetime object for the following year.
Finally, use 'format' to output.
$d = new DateTime(date('Y').'-08-05');
if ($d < new DateTime()) {
$d = new DateTime((date('Y')+1).'-04-15');
}
echo $d->format('d/m/Y');
You can achieve this using this tutorial. You can define time zone and display the date as per your format.
Check this manual. http://php.net/manual/en/function.date.php
clear examples are given here:
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
echo $today = date("d/m/y"); // 03/10/01
?>

can I get the month from the time stamp if it is possible how canI do that?

When a image is uploaded to the site I want to display the month which it was uploaded under the picture. I have a time-stamp field in the database.
Can I get the month from the time stamp if it is possible how can I do that?
I tried to do it using this code
<?php $query="SELECT * FROM magimgdes ORDER BY id DESC, time DESC " ;
$result=mysql_query($query) OR DIE(mysql_error());
while($row=mysql_fetch_array($result)){
$value1=$row['image'];
$value3=$row['time'];
?>
<img src="admin/upload/hompageimage/<?php echo $value1; ?>" width="180" height="195" alt="magimage" />
<?php echo $value3;} ?></div>
The "time" field is the timestamp field in database.
I just want to echo only the month.
Yes , you can get by date()
$weekday = date('N', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
You can do that with the date function.
http://php.net/manual/en/function.date.php
First you will need to convert the MySql datetime format to UNIX timestamp, to do that you can use strtotime function. Then you can use many functions to deal in the extraction of the month, for example getdate function.
$timestamp = strtotime($row['time']); // Convert to unix time.
$info = getdate($timestamp); // Get the fields of a date (in an array).
$month = $info['mon']; // This is the element that contains the month.
echo $month; // This will print a number from 1 to 12.

Comparing a date to current server date using 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']);

Categories