Need get info, is EndDate expired or not (EndDate is row, where typed info about date of buyed user premium)
connectuser2db();
$bile_sql = "SELECT EndDate FROM tbl_rfuser WHERE Serial = '" . $userdata['serial'] . "'";
if (!($bile_result = mssql_query($bile_sql))) {
echo "Unable to select query the Billing table";
exit;
}
$bilel = mssql_fetch_array($bile_result);
$userdata['Enddate'] = $bilel['EndDate'];
$t_logd = $bilel['EndDate'];
echo $t_logd;
result like Jul 14 2020 02:36:00:000PM (if EndDate in smalldatetime data type)
$time = date("Y-m-d H:i:s");
echo $time;
result like 2020-07-15 14:51:21
If i use $time in this row, it update enddate to curent time:
$t_login = $userdata["lastlogintime"]; // last user login in game (from mssql) Jul 15 2020 02:36:00:000PM
$t_logd = $bilel['EndDate']; // Premium EndDate (from mssql) Jul 14 2020 02:36:00:000PM
$time = date("Y-m-d H:i:s"); // current time (from php) like 2020-07-15 14:51:21
if( $t_logd <= $t_login )
{ connectuser2db();
$user_update = "UPDATE tbl_rfuser SET EndDate='" . $time . "' WHERE Serial= '" . $userdata["serial"] . "'";
mssql_query($user_update); }
It succesfully compare "lastlogin" and "enddate", and if account is new or user be online last time before EndDate expired - change EndDate to current time, that succesfully takes from $time.
But if user login after EndDate, it not works
Want this compare:
if( $t_logd <= $t_login ) //working now
Change to compare like:
if( $t_logd <= $time ) // not working now
Help please
How can I take current time from mssql or change $time for something, that can compare?
Tried strtotime, but can't do it (sql server check it in miliseconds, but my php old and can't use date("Y-m-d H:i:s:v") format)
Note, that the MSSQL PHP extension is not available anymore on Windows with PHP 5.3 and removed in PHP 7.0.0, but if you want a solution, you may try the following:
Specify how the datetime values are returned from the SQL Server by setting the mssql.datetimeconvert option in your php.ini file to Off. When this option is On the datetime values are returned converted to SQL server settings. When this option is Off the datetime values are returned in YYYY-MM-DD hh:mm:ss format.
Use date_create() to create and compare the dates as PHP datetime objects.
The next simplified example demonstrates this approach (tested with PHP 5.2.9):
<?php
// Connection
$server = 'server,port';
$username = 'username';
$password = 'password';
$database = 'database';
$conn = mssql_connect($server, $username, $password);
if (!$conn) {
echo "Error (sqlsrv_connect): ".mssql_get_last_message();
exit;
}
mssql_select_db($database, $conn);
// Statement
$sql = "SELECT DATEADD(day, -1, GETDATE()) AS [DateTime]";
$stmt = mssql_query($sql, $conn);
if (!$stmt) {
echo "Error (sqlsrv_query): ".mssql_get_last_message();
exit;
}
// Results
while ($row = mssql_fetch_assoc($stmt)) {
$datetime1 = date_create($row['DateTime']);
}
$datetime2 = date_create();
// Compare dates
echo ($datetime1 <= $datetime2) ? 'Result: <=' : 'Result: >';
// End
mssql_free_result($stmt);
mssql_close($conn);
?>
Just use GETDATE:
UPDATE tbl_rfuser SET EndDate=GETDATE() WHERE Serial= ...
Related
I need to extract only the day number of a user's registration date.
And extract only the day number of the current date.
Simply in an if loop, say if the day number the user registered is equal to the day number of the current date, do this, or do that.
Code:
$manager = "Manager";
$managerPRO = "ManagerPRO";
$q = $connessione->prepare("
SELECT * FROM collaboratori
WHERE cat_professionisti = ?
OR cat_professionisti = ?
");
$q->bind_param('ss', $manager,$managerPRO);
$q->execute();
$r = $q->get_result();
while($rr = mysqli_fetch_assoc($r)){
/*REGISTRATION DATE*/
$registrazione = $rr['data_registrazione'];
$timestamp = strtotime($registrazione);
echo date("d", $timestamp) .'=' ;
/*CURRENT DATE*/
$data_corrente = date('Y-m-d');
$timestamp_uno = strtotime($data_corrente);
echo date("d", $timestamp_uno);
/*CONTROL*/
if ($timestamp == $timestamp_uno){
echo "yes".'<br>';
}else{
echo "no".'<br>';
}
}
Result:
18=18no
17=18no
16=18no
16=18no
Why in the first case if 18 = 18 gives me false?
However, if I change the date of the user's registration and therefore the first 18, from 2020/11/18 to 2020/12/18, then the current month gives me yes!
I need that regardless of the month, just by checking the day if it is the same, tell me yes, where am I wrong?
You are comparing timestamps, which are measured in seconds. What you are doing is effectively comparing two different points in time, not the days of the month.
You really should be using DateTime. If you want to compare only the day part then you can do something like this.
$dt1 = new DateTime($registrazione);
$dt2 = new DateTime(); // defaults to now
if($dt1->format('d') === $dt2->format('d')) {
echo "Yes, it's the same day of the month";
} else {
echo 'no!';
}
Im using the following mysql script to display a list of active / upcoming fixtures.
Select * FROM schedule
WHERE schedule.gameDate > CURDATE() OR
( schedule.gameDate = CURDATE() and schedule.gameTime > CURTIME() )
GROUP BY schedule.tournament
ORDER By schedule.gameDate
The above script works perfectly.
However, as an additional check to prevent a user from accessing a fixture which has expired im doing the following.
$curTime = date('H:i:s');
$curDate = date('Y-m-d');
$sql = "SELECT * FROM schedule
WHERE tournament = :tour AND weekNum = :round";
$stmnt = $db->prepare($sql);
$stmnt->bindValue(':tour',$tournament);
$stmnt->bindValue(':round', $round);
$stmnt->execute();
$results = $stmnt->fetchAll();
foreach($results as $result){
echo $eventDate = $result['gameDate'];
echo $startTime = $result['gameTime'];
if($curDate > $eventDate){
echo '<h1> CURRENT ROUND ALLREADY STARTED</h1>';
die();
}//if
else if($eventDate==$curDate && $curTime>$startTime){
echo '<h1> CURRENT ROUND ALLREADY STARTED</h1>';
die();
}//else if
}//foreach
My Problem.
The loop never passes the first IF statment which always results to true...
DB Table
When I echo the variables I get the following:
$curTime = 09:30:00
$curDate = 2017-19-03
$eventDate = 2017-03-21
$startTime = 13:00:00
I realize it is not the prettiest code but according to my little experience and logic it should pass both if statments...
Any advise appreciated
Use strtotime() in compare two date in php
Replace if($curDate > $eventDate) with if(strtotime($curDate) > strtotime($eventDate)) and other comparison also
You can convert them to DateTime objects or timestamps to compare. String comparison will only check whether they are equal or not.
So:
$now = new DateTime();
$event = new DateTime($eventDate . ' ' . $startTime);
Then you can check whether dates are equal or later the same way you're already doing. See Example #2 on the Date Diff page of the php website. E.g. $now > $event, etc.
It should be elseif.not else if. There should not be a space between else and if.
Hi friends i have a php program where user can upload excel file and data w illbe stored in php database, it works as i expected but the problem is date stored in database is totally different from what i entered in excel
i have used the following codes to read the value from excel..
include("excel/reader.php");
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read($_POST['upload']);
$conn = mysql_connect("localhost","root","");
mysql_select_db("mydatabase",$conn);
$a="TRUNCATE TABLE `sheet`";
mysql_query($a);
for ($x = 2; $x <= count($data->sheets[0]["cells"]); $x++)
{
$inv = $data->sheets[0]["cells"][$x][1];
$ds = $data->sheets[0]["cells"][$x][2];
$dosstr=strtotime($ds);
$dos=date('Y-m-d',$dosstr);
$ptname = $data->sheets[0]["cells"][$x][3];
$bal = $data->sheets[0]["cells"][$x][4];
$proname = $data->sheets[0]["cells"][$x][5];
$sql = "INSERT INTO sheet (INV,DOS,PTNAME,BAL,PROV)
VALUES ('$inv','$dos','$ptname','$bal','$proname')";
mysql_query($sql);
}
header('location:exsheet2.php');
?>
i have the following dates stored in excel
9/1/2014,
28/12/2013 ,
27/9/1991 ,
1/1/2015 ,
31/12/2014 ,
1/1/2012 ,
5/9/2001 .
but what is being stored in database is
1970-01-01 ,
1970-01-01 ,
1970-01-01 ,
1970-01-01 ,
1970-01-01 ,
1970-01-01 ,
1970-01-01 ,
1970-01-01.
please assist me with this issue thanks
for ($x = 2; $x <= count($data->sheets[0]["cells"]); $x++)
{
$inv = $data->sheets[0]["cells"][$x][1];
$ds = $data->sheets[0]["cells"][$x][2];
echo $ds;
echo "<br>";
if(ctype_digit($ds))
{
date_default_timezone_set("Asia/Kolkata");
$dDate = date_create('1900-01-01');
$dDate = date_modify($dDate, '+'.($ds-2).' day');
if($dDate!="")
{
$dos = date_format($dDate,'Y-m-d');
echo "IF".$dos;
echo "<br>";
}
else
{
$dos=$ds; //not able to convert date so show it directly
echo "Else".$dos;
echo "<br>";
}
}
else
{
$dos=$ds;
echo"if Else".$dos;
}
$ptname = $data->sheets[0]["cells"][$x][3];
$bal = $data->sheets[0]["cells"][$x][4];
$proname = $data->sheets[0]["cells"][$x][5];
$sql = "INSERT INTO sheet (INV,DOS,PTNAME,BAL,PROV)
VALUES ('$inv','$dos','$ptname','$bal','$proname')";
mysql_query($sql);
}
Found the actual problem is in your date function. You cannot use these php functions coz these are for unix timestamp. And excel returns date as integer values. The no of days since 1/1/1900, including first and last day
so replace these lines:
$dosstr=strtotime($ds);
$dos=date('Y-m-d',$ds);
to
date_default_timezone_set("Asia/Kolkata");
$dDate = date_create_from_format('Y-m-d', '1900-01-01');
$dDate=date_add($dDate, date_interval_create_from_date_string(($ds-2).' days'));
$dos=$dDate->format('Y-m-d');
PHP 5.2 version:
date_default_timezone_set("Asia/Kolkata");
$dDate = date_create('1900-01-01');
if($dDate = date_modify($dDate, '+'.($ds-2).' day'))
$dos = date_format($dDate,'Y-m-d');
else $dos=$ds; //not able to convert date so show it directly
OR safe code:
if(ctype_digit($ds)){
date_default_timezone_set("Asia/Kolkata");
$dDate = date_create('1900-01-01');
$dDate = date_modify($dDate, '+'.($ds-2).' day');
if($dDate!="") //if($dDate) //is_a($dDate,'DateTime')) //get_class($dDate)=='DateTime' //if ($dDate instanceof DateTime)
$dos = date_format($dDate,'Y-m-d');
else $dos=$ds;
}
else $dos=$ds;
And reason of problem of getting incorrect date which have day greater than 12 is: You have dates stored in excel as Indian dates. But your laptop's default date and time are US or UK...
you need to change it first (in Region and Language Settings).
1) click on time shown at right of taskbar, in windows.
2) under the calendar you'll see a link "change date and time settings". open it.
3) click "change date and time" button -> then click change calendar settings link.
4) probably two more window will open. close the customized format window and go to "Region and Language".
5) Select "English(India)" as Format and "dd-MM-yyyy" as short date.
6) click OK. Then try again, if any problem, comment.
Print the sql query and check what values are inserted in the query. The date format should be "Y-m-d".
Just remove all the formatting from the date column of the excel sheet. Then try again. This may help you PHP Spreadsheet_Excel_Reader always reading date as Nov 30, 1999, http://dirtyhandsphp.blogspot.in/2012/03/phpexcelreader-uses-and-problems.html
I'm having some trouble calculating if time is past 15 minutes, on a date and time in my database. How can I check this?
I have this in my database (example):
2014-01-30 15:29:31
And then I get the date from PHP:
$date = date("Y-m-d H:i:s");
But how can I check if the time has passed 15 minutes, or if it's a different day?
I have this code so far:
$date = date("Y-m-d H:i:s");
$sql = "SELECT date FROM reset WHERE session_id = '".$sessid."' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$dbdate = $value->date;
$checkdate = strtotime($dbdate);
if ($checkdate - time() > 15 * 60) {
error_log("15 mins passed");
}
You can convert the date to a timestamp with strtotime (which supports the MySQL date format) and then compare it to the current timestamp from time.
$dbtimestamp = strtotime($datefromdb);
if (time() - $dbtimestamp > 15 * 60) {
// 15 mins has passed
}
To compare the dates, you can use date to get the year/month/day from the timestamp and then compare them against the current date.
if (date("Y-m-d", $dbtimestamp) != date("Y-m-d")) {
// different date
}
Using a DateTime object:
$dateTimeObject = new \DateTime($dateString);
//and subtract using an interval
$dateTimeObject->sub(new \DateInterval("PT15M"));
$newDateString = $dateTimeObject->format("Y-m-d H:i:s");
you cans use DATE_FORMAT functions.
$actual_minute = date("i");
$data = mysql_query("SELECT DATE_FORMAT(date,'%i') minutes FROM reset WHERE session_id = '".$sessid."'");
$database_minute = $data{'minutes'};
I have tried every combo I can think of / found and no matter what I do, my codet echos the message even if the account isn't locked out:
<?php
$infosql = "SELECT * FROM premiersounds_users WHERE customer_id = $id";
$inforesult = mysql_query($infosql) or die(mysql_error());
$info = mysql_fetch_array($inforesult);
//Get current date from server
$format="%m/%d/%y";
$c_date=strftime($format);
//set sessions
$_SESSION['current_date'] = $c_date;
//The date in the database is 10/31/11
$_SESSION['lockout_date'] = $l_date;
//Check is Current date = lockout date
if ($c_date <= $l_date) {
header("location:documnet_editors/edit_weddingplanner.php?id=$id");
}
else {
echo 'Whoops! Were sorry your account has been locked to edits
because your event is less than 48 hours from now or your event has passed.
To make changes to your event please contact your DJ.';
}
?>
<?php
//Destroy Session for Lockout Date to prevent bypasses
unset($_SESSION['lockout_date']);
?>
If your $l_date is populated, and I don't think it is, if it is stored as MM/DD/YY, you'll want to use PHP's strtotime to convert it to a unix timestamp for quick comparison:
if( strtotime($db_date) >= time() )
{
// do something
}
I would suggest comparing timestamps instead of formatted dates:
<?php
$date_a = new DateTime();
$date_b = new DateTime('2000-10-20 00:10:20');
if ($date_a->getTimestamp() > $date_b->getTimestamp()) {
echo 1;
} else {
echo 0;
}
convert your dates to unixtime for more accurate comparison. Add this function to your code:
function unix_time($date){
$unix_date = str_replace("-","/",$date);
$unix_date = str_replace(".","/",$unix_date);
$unix_date = str_replace(" pm","",$unix_date);
$unix_date = str_replace(" am","",$unix_date);
$time = strtotime($unix_date);
return $time;
}
then convert the dates to unix:
$l_date = unix_time($_SESSION['lockout_date']);
$c_date = unix_time($_SESSION['current_date']);
or you can also get the date directly from the database:
$l_date = unix_time($info['date_in_database']);
compare the dates in unix format:
if ($c_date = $l_date) {
// your code here
}
this should work.