how can i check a date with current date - php

if ((strtotime("1 June") == time()))
{
mysql_connect("localhost","root","");//database connection
mysql_select_db("student");
$order = "UPDATE stud SET class='9' WHERE class='8'";
$result = mysql_query($order); //order executes
}
the above code does not work..i changed my date to 1 june..but strtotime() and time() returns different values..

$cmp = strtotime("19 April");
// convert specified date to int
$current_date = strtotime(date("Y-m-d"));
if ($cmp == $current_date) {
// process
}
the time() function will give you and equivalent of
strtotime(date('Y-m-d H:i:s'));
if want to compare the current date and the date given use only
strtotime(date('Y-m-d'));
that will give result to the format of
strtotime("19 April");
check documentation of strtotime and date
http://php.net/manual/en/function.strtotime.php

you are trying to check the time against a date see php time function
try using the date function instead date('jS F');
again see php date function

Related

Date difference in PHP code does not execute

I have a code in PHP 5.5.11 where I am trying to do the following:
Get today's date in a variable --> $today
Calculate the end of month from a date in a form --> $st_dt_eom
if difference between these 2 dates is more than 5 days then execute a code. The code in the if condition below does not execute.
$today= date();
if($_POST['Submit']=='SAVE')
{
$st_dt=YYYYMMDD($_POST['st_dt'],"-");
$st_dt_eom= datetime::createfromformat('YYYYMMDD',$st_dt);;
$st_dt_eom->modify('last day of this month');
$diff = $today->diff($st_dt_eom);
$diffDays= intval($diff->format("%d")); //to get integer number of days
if($diffDays>5){
redirect("index.php");
}
}
An example:
// 2022-12-19
$today = date('Y-m-d');
// $_POST['st_dt']
$st_dt = '2022-12-31';
function dateDiffDays($today, $st_dt)
{
$today_obj= DateTime::createfromformat('Y-m-d', $today);
$st_dt_eom= DateTime::createfromformat('Y-m-d', $st_dt);
$diff = $today_obj->diff($st_dt_eom);
return $diff->days;
}
// int(12)
$res = dateDiffDays($today, $st_dt);
use var_dump to locate your bug.
A few suggestions to improve your code and produce something workable:
<?php
// Instead of using date(), use a DateTime() then you're comparing two DateTimes later on
$today = new DateTime();
// I'm assuming that your YYYYMMDD function removes "-" from the $_POST['st_dt']
// to provide a date in the format YYYYMMDD (or Ymd in PHP).
// Unfortunately, DateTime doesn't understand that format.
// So I'd change this for keeping Y-m-d (YYYY-MM-DD).
// Or modify your code to return that format!
$st_dt = $_POST['st_dt'];
// Watch out for your cAsE when using PHP functions!
$st_dt_eom = DateTime::createFromFormat('Y-m-d',$st_dt);
$st_dt_eom->modify('last day of this month');
$diff = $today->diff($st_dt_eom);
$diffDays= intval($diff->format("%d"));
if($diffDays > 5){
redirect("index.php");
}

How to force php strtotime to return today date if date variable is empty?

I have a js calendar in an HTML page and I'm passing the picked date into a PHP file. Currently, the date field is mandatory and it will display the default 1970/01/01 if no date is picked in the calendar.
I'm trying to change the behavior so that when no date is picked, then the current date will be passed into the PHP file.
The code in the PHP file is the one below:
$mydate = strtotime($_POST['mydate']);
$mydate = date("Y/m/d",$mydate);
How can I change it in order to display the current date if there's no value passed from $_POST['mydate']?
PS: I'm a newbie to PHP functions and searching for similar cases in the forums did not help.
The reason you get that is because strtotime returns false if it can't parse the date.
False is typecasted to 0, and date of 0 is 1970.
What you can do is to check if the strtotime return is false. (Not the same as isset or 0 in my opinion).
$mydate = strtotime($_POST['mydate']);
if($mydate === false){ // === is strict comparison
$mydate = date("Y/m/d");
}else{
$mydate = date("Y/m/d",$mydate);
}
Try this:
if( empty( $_POST['mydate'])){
$mydate = date("Y/m/d");
} elseif( FALSE !== $tm = strtotime( $_POST['mydate'])) {
$mydate = date("Y/m/d", $tm);
} else {
$mydate = date("Y/m/d");
}

PHP Checking if outputted date is less than current date

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>';

How to check whether the date coming in $_POST array is not greater than today's date in PHP?

The date to be checked is as follows :
$submission_date = 12-25-2014; //The date in mm-dd-yyyy format that is to be tested against today's date
Now I want to echo the error message since the date contained in a variable $submission_date is a future date.
How should I do this efficiently and effectively using PHP?
Thanks in advance.
Many ways to do this (use DateTime::createFromFormat() to control exact format of input dates, for example) but perhaps the simplest that suits the example is:
$isFuture = (strtotime($submission_date) > strtotime($_POST['current_date']))
Note that OP changed the question. If desired date to test against is not in $_POST array, just replace strtotime($_POST['current_date']) with time() to use current system time.
To compare against current date, disregarding time of day, use:
$today = new DateTime(date("Y-m-d"));
// $today = new DateTime("today"); // better solution courtesy of Glavić
// see http://php.net/manual/en/datetime.formats.relative.php for more info
$today_timestamp = $today->getTimestamp();
If posted format is in m-d-Y, then you cannot convert it to unix timestamp directly with strtotime() function, because it will return false.
If you need to use strtotime() then change the input format to m/d/Y by simple str_replace().
On the other hand, you could use DateTime class, where you can directly compare objects:
$submission_date = DateTime::createFromFormat('!m-d-Y', $submission_date);
$today_date = new DateTime('today');
if ($submission_date > $today_date) {
echo "submission_date is in the future\n";
}
demo
With PHP DateTime you can check whether the input date is future or old w.r.to the todate.
$submission_date = DateTime::createFromFormat('m-d-Y', $submission_date);
$submission_date = $submission_date->format('Y-m-d');
$current_date = new DateTime('today');
$current_date = $current_date->format('Y-m-d');
if ($submission_date > $current_date)
{
echo "Future date";
}
else
{
echo "Old date";
}

php check date if month is equal to something

So i fetched the date into MSSQL's datetime type field.
E.g 2014-01-23 06:54:49.647
I need a simple way to determine if the datetime corresponds a specific month in a condition in PHP
//i wanted something like this
$getthedate = odbc_exec($connection, "SELECT date FROM sometable WHERE ..somecondition..");
$datedata = odbc_result($getthedate, 'DATE');
if($datedata = /* is january */) {
//do something
}
You can convert the time to a UNIX timestamp by using strtotime:
$timestamp = strtotime($datedata); //2014-01-23 06:54:49.647 = 1390460089
Then convert the timestamp into whatever format you want with the 'date' function:
$month = date('F', $timestamp); // Outputs: January
Then you can use $month to compare in your if statement as it returns the month of the date.
if ($month == "January") {
//do something
}
If you have a variable containing the date time string
2014-01-23 06:54:49.647
and you know it'll be in that format, simply use http://php.net/substr to get the month.
(This ignores any time zone issues since you didn't mention that.)
try this
<?php
$timestamp="2014-01-23 06:54:49.647";
$month = date('F', $timestamp);
$current_month=date('F');
if($month==$current_month)
{
//do something
}
?>
OR
<?php
$timestamp="2014-01-23 06:54:49.647";
$month = date('m', $timestamp);
$current_month=date('m');
if($month==$current_month)
{
//do something
}
check this
$datedata = "2014-10-19 12:36:00";
if(strtolower(date('F',strtotime($datedata))) == strtolower(date('F'))){
echo "success";
}else{
echo "unsucess";
}

Categories