my salary month start in previous month 26th and ends in this month 25
ie February month is 2016-01-26 to 2016-02-25.
i need the out put as below
$date = '2014-02-27';
echo date('F', strtotime($date)); = "March" //if date > 25 print next month
I believe what you have to do is get the day:
$date = '2014-02-27';
$day = date('d', strtotime($date));
Then see if bigger than 26, increase one month
$month = date('F', strtotime($date));
if($day > 26)
{
$ts = mktime(0, 0, 0, date("n", strtotime($date)) + 1, 1);
$tmpNewDate = date("Y-m-d H:i:s", $ts);
$month = date('F', strtotime($date));
}
echo $month;
Try this one, I have prepared several date inputs :
<?php
$input_date = '2014-2-2';
// $input_date = '2014-2-27';
// $input_date = '2014-12-27';
$date = DateTime::createFromFormat('Y-m-d', $input_date);
$next_month = null;
$year = $date->format('Y');
$next_month = $date->format('m');
if ($date->format('d') >= 26)
$next_month = intval($date->format('m')) + 1;
if ($next_month == 13){
$next_month = 1;
$year = intval($year) + 1;
}
echo "next month = " . $next_month . ", year = " . $year;
?>
Related
I have 5 Admin in the database. So now I want to get the last user logged in date and time detail. Who worked before me.means after my logged in the dashboard I want to see previous USER login and logout time and date.
I am able to get my (current user) login date and time. But I don't have an idea how can I get previous user(Admin) login and logout date/time detail.
I am sending my login.php code.Where i wrote current user time/date code.Please see the code and give me suggestion how can i get previous user login and logout date/time.
Thanks.
<?php include "db.php"; ?>
<?php session_start(); ?>
<?php
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysqli_real_escape_string($connection,$username);
$password = mysqli_real_escape_string($connection,$password);
$password = md5($password);
$login_query = "SELECT * FROM users WHERE user_username = '$username'" ;
$result_query = mysqli_query($connection,$login_query);
$count = mysqli_num_rows($result_query);
if(!$count){
die("QUERY FAILED". mysqli_error($connection));
}
while($row= mysqli_fetch_array($result_query)){
$log_user_id = $row['user_id'];
$log_user_username = $row['user_username'];
$log_user_password = $row['user_password'];
$log_user_firstname = $row['user_firstname'];
$log_user_lastname = $row['user_lastname'];
$log_user_role = $row['user_role'];
$log_user_time = $row['time'];
$log_user_ip = $row['ip'];
}
if($username !== $log_user_username && $password !== $log_user_password){
header ("location: ../index.php");
}elseif($username == $log_user_username && $password == $log_user_password){
if($count == 1) {
$_SESSION['username']=$log_user_username;
$_SESSION['firstname']=$log_user_firstname;
$_SESSION['lastname']=$log_user_lastname;
$_SESSION['user_role']=$log_user_role;
$_SESSION['last_login'] = $log_user_time;
$_SESSION['last_login_ip'] = $log_user_ip;
date_default_timezone_set("Asia/Kolkata");
$current_date = date("F d, Y, h:i:s A");
$ip = $_SERVER['REMOTE_ADDR'];
$query = "UPDATE users SET time= NOW() ,ip='$ip' WHERE user_username='$username'";
mysqli_query($connection,$query);
$last_login_date = $_SESSION['last_login'];
$last_login_date2 = date('F d, Y, h:i:s A');
$diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
$year = floor($diffs / (365*60*60*24));
$month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
$day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
$hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60));
$minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60);
$second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));
if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second < 30 ) {
$time1 = 'Just now';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) {
$time1 = 'few seconds ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
$time1 = '1 minute ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
$time1 = $minute . ' minutes ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) {
$time1 = '1 hour ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 ) {
$time1 = $hour . ' hours ago';
} elseif($year == 0 AND $month == 0 AND $day == 1 ) {
$time1 = 'Yesterday';
} elseif($year == 0 AND $month == 0 ) {
$time1 = $day . ' days ago';
} elseif($year == 0 AND $month == 1 ) {
$time1 = '1 month ago';
} elseif($year == 0) {
$time1 = $month . ' months ago';
} elseif($year == 1 ) {
$time1 = '1 year ago';
} else {
$time1 = $year . ' years ago';
}
}
if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
$last_login_ip = "from this IP address (".$_SERVER['REMOTE_ADDR'].")";
}
else {
$last_login_ip = "IP address ".$_SESSION['last_login_ip'];
}
}
header ("location: ../admin");
}else{
header ("location: ../index.php");
}
?>
So after my logged in on Dashboard (index.php) the page i did echo the variables and i can able to see current user login date/time detail.Which is my login detail.
Please give me suggestion how can i get previous user login and logout detail.
Thanks
notification.php
<span class="pull-right text-muted small"><em></em></span></a>
<?php
date_default_timezone_set("Asia/Kolkata");
$last_login_date = date("F d, Y, h:i:s A");
$last_login_date = $_SESSION['last_login'];
$last_login_date2 = date('F d, Y, h:i:s A');
$diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
$year = floor($diffs / (365*60*60*24));
$month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
$day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
$hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60));
$minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60);
$second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));
if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second < 30 ) {
$time1 = 'Just now';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) {
$time1 = 'few seconds ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
$time1 = '1 minute ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
$time1 = $minute . ' minutes ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) {
$time1 = '1 hour ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 ) {
$time1 = $hour . ' hours ago';
} elseif($year == 0 AND $month == 0 AND $day == 1 ) {
$time1 = 'Yesterday';
} elseif($year == 0 AND $month == 0 ) {
$time1 = $day . ' days ago';
} elseif($year == 0 AND $month == 1 ) {
$time1 = '1 month ago';
} elseif($year == 0) {
$time1 = $month . ' months ago';
} elseif($year == 1 ) {
$time1 = '1 year ago';
} else {
$time1 = $year . ' years ago';
}
if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
$last_login_ip = "From this IP address (".$_SERVER['REMOTE_ADDR'].")";
}
else {
$last_login_ip = "IP address ".$_SESSION['last_login_ip'];
}
?>
<a href="#" class="list-group-item">
<i class="fa fa-user"></i> User Name
<span class="pull-right text-muted small"><em><?php echo $_SESSION['username']; ?></em>
</span>
</a>
<a href="#" class="list-group-item">
<i class="fa fa-sign-in"></i> Last Login Time
<span class="pull-right text-muted small"><em><?php echo $time1; ?></em>
</span>
</a>
<a href="#" class="list-group-item">
<i class="fa fa-sign-out"></i> Last Logout Time
<span class="pull-right text-muted small"><em>27 minutes ago</em>
</span>
</a>
<a href="#" class="list-group-item">
<i class="fa fa-tasks fa-fw"></i> User Ip
<span class="pull-right text-muted small"><em><?php echo $last_login_ip; ?></em>
</span>
</a>
here iam getting admin login date and time detail.But i unable to get previous visitor login detail . i can just see only my login time/date detail after login in here.
So please help.
I can give you the basic logic for this. In this you can set the session variable that it will act as flag.
// start session
session_start();
// if not yet logged in update database
if(!isset($_SESSION['logged']))
{
// update your database here
$query = "UPDATE table
SET LastLogin=now()
WHERE ID='$ID'";
$result = mysql_query($query); // etc...
// if the table was updated
if($result === true)
{
// then create a session var
$_SESSION['logged'] = 1;
}
}
OR
You can insert the user id, date and time at the time of login and get it back as last id that will be your previous login.
Create one activity table in database. Click the link to view example here->
User Activity Table Structure
Every time when any user logs in, just enter the login time and user id along with other required details in the activity table. At the time of login you can change the login status to deactivated for that particular session.
Next time when any user logs in you can get the last login time from database and current time from PHP date() function. Just get the time difference between those 2 dates and you will get the last login time and duration by previous user.
That's it.
Hope this would work.
Javascript - functions enable switch months
function goLastMonth(month, year)
{
if (month == 1)
{
--year;
month = 13;
}
--month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1)
{
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
function goNextMonth(month, year)
{
if(month == 12)
{
++year;
month = 0;
}
++month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1)
{
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
</script>
PHP - creating calendar
if (isset($_GET['day']))
{
$day = $_GET['day'];
}
else
{
$day = date("j");
}
if(isset($_GET['month']))
{
$month = $_GET['month'];
}
else
{
$month = date("n");
}
if(isset($_GET['year']))
{
$year = $_GET['year'];
}
else
{
$year = date("Y");
}
$currentTimeStamp = strtotime( "$day-$month-$year");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<table border='1'>
<tr>
<td><input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.",".$year?>)"></td>
<td colspan='5'><?php echo $monthName.", ".$year; ?></td>
<td><input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.",".$year?>)"></td>
</tr>
<tr>
<td width='50px'>Poniedziałek</td>
<td width='50px'>Wtorek</td>
<td width='50px'>Środa</td>
<td width='50px'>Czwartek</td>
<td width='50px'>Piątek</td>
<td width='50px'>Sobota</td>
<td width='50px'>Niedziela</td>
</tr>
<?php
echo "<tr>";
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay-1; $j++, $counter++)
{
echo "<td> </td>";
}
}
if($counter % 7 == 0)
{
echo"</tr><tr>";
}
$monthstring = $month;
$monthlength = strlen($monthstring);
$daystring = $i;
$daylength = strlen($daystring);
echo "<td align='center' ";
echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."'>".$i."</a></td>";
}
echo "</tr>";
?>
</table>
The calender works well, but when the first day in the month is Sunday, then it starts to count the days from Monday (for example May 2016). How do I fix this?
Try the following code
if($i == 1)
{
$firstDay = date("w", $timeStamp) - 1;
if ($firstDay < 0)
$firstDay = 6;
// Now monday is 0, and sunday is 6
for($j = 0; $j < $firstDay; $j++, $counter++) // remove the -1 in $firstDay-1
{
echo "<td> </td>";
}
}
The first column of your calendar is Monday, and the last column is Sunday. $firstDay = date("w", $timeStamp) returns a number from 0-6 where 0 is Sunday, and 6 is Saturday. So I change it so that 0 is Monday, and 6 is Sunday.
So now the loop for($j = 0; $j < $firstDay; $j++, $counter++)
If Monday, then since it's the first column you don't want the loop to run. On Monday, $firstDay is 0 so the loop doesn't run. If Tuesday, then $firstDay is 1 and the loop runs once to make the empty space for Monday.
I want a custom time algorithm like javascript. I was able to achieve this using php but am not able to do the same with javascript!
WHat i had in php was like.....
PHP FIDDLE(i want something like this in js output)
function getTime($mytime){
// $mytime is a datetime sql database format time
$thetime = strtotime($mytime);
$q = new DateTime($mytime);
$current_time = date('Y-m-d H:i:s',time());
$w = new DateTime($current_time);
$result = $w ->diff($q);
$string = "";
$year = $result->y;
$month = $result->m;
$day = $result->d;
$hour = $result->h;
$min = $result->i;
$sec = $result->s;
if($year==0){
if($month==0){
if($day==0){
if($hour==0){
if($min==0){
if($sec <30){
$string .= "Just now";}else if($sec<60){ $string .= "A few seconds ago";}
}else{
if($min<5){$string .="A few minutes ago";}else if($min>=5){$string .=$min . ' minutes ago'; }
}}else{
if($hour<6){$string .=$hour . ' hours ago';}else if($hour>=6){
$chour = (date('H',$thetime));
if( ($chour>=5)&&($chour<=17) ){$string .= 'Today at ' .date('h.m a',$thetime);}else{$string .= 'Tonight at '. date('h.m a',$thetime);}}
}}else{
if($day==1){$string .= "Yesterday at " . date('h:m a',$thetime);}else if(($day>1) && ($day<=7)){$string .= $day . " days ago";}else if($day>7){$string .= date('M, d',$thetime) . ' at '. date('h:i a',$thetime);}
}}else{
if($month<5){$string .=$month . ' months ago'; }else if($month>=5){$string .= date('M,d',$thetime) .' at ' . date('h.i a',$thetime) ;}
}}else{
$string .=date('dS M , Y',$thetime) .' at '. date('h.i a',$thetime);
}
return $string;
}
What i thought was to dynamically update time using javascript! so i did this......
$(function(){
var $second = 1000;
var $minute = $second * 60;
var $hour = $minute * 60;
var $day = $hour * 24;
setInterval(function(){
$('.comment').each(function(){
var $this = $(this);
var $time = new Date($this.attr('datetime'));
var $now = new Date();
var $distance = $now - $time;
var $days = Math.floor($distance / $day);
var $hours = Math.floor(($distance % $day) / $hour);
var $minutes = Math.floor(($distance % $hour) / $minute);
var $seconds = Math.floor(($distance % $minute) / $second);
var $time_string = '';
if($days > 0) {
$time_string = $time_string + $days + ' Day(s) ';
}
if($hours > 0) {
$time_string = $time_string + $hours + ' Hour(s) ';
}
if($minutes > 0) {
$time_string = $time_string + $minutes + ' Minute(s) ';
}
if($seconds > 0) {
$time_string = $time_string + $seconds + ' Second(s)';
}
$this.text($time_string);
});
}, 1000);
});
but his outputs something like this fiddle here.
What i wanted is like my prev. php output i.e X min ago or X seconds ago or 12 May 2014 etc how do i implement my above php code with this js ?
Well, I'm making a game, and in my game I have a countdown script in javascript that receives the date when the building upgrade is over, and makes a countdown, and when the countdown ends, executes a script that upgrades the building to the next level.
But just works in google chrome, and in the other browsers appears like that:
Firefox:
Google Chrome
Just the Javascript:
date_default_timezone_set('europe/lisbon');
$datephp = date('Y-m-d H:i:s');
echo'
<script type="text/javascript">
function cdtd() {
var xmas = new Date("' . $factory_date . '");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
clearTimeout(timer);
$("#factory_upgrade").load("/include/factory_upgraded.php");
$("#quantidade_fabricas").load("/include/factory_stats.php");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;' .
"var tempo=('0' + hours).slice(-2)+':'+('0' + minutes).slice(-2)+':'+('0' + seconds).slice(-2);"
.
'
document.getElementById("secsBox").innerHTML = tempo;
var timer = setTimeout("cdtd()",1000);
}
</script>
';
Complete function:
function factory_update($get){
$userid = $_SESSION['userid'];
$query00 = "SELECT * FROM factory_upgrading WHERE userid = '$userid'";
$result00 = mysql_query($query00) or die(mysql_error());
while($row00 = mysql_fetch_array($result00)){
$factory_upgrade = $row00['userid'];
}
if(!isset($factory_upgrade)){
echo "Sem melhoramentos.";
return 0;
}
$query01 = "SELECT * FROM factory_upgrading WHERE userid = '$userid'";
$result01 = mysql_query($query01) or die(mysql_error());
while($row01 = mysql_fetch_array($result01)){
$factory_level = $row01['new_level'];
$factory_date = $row01['upgraded'];
}
if ($get == "load")
{
echo '<div class="message_upgrades" ">';
echo '<div class="loading"><img src="/images/loading.gif"></img></div>';
echo '</div>';}
else
{
date_default_timezone_set('europe/lisbon');
$datephp = date('Y-m-d H:i:s');
echo'
<script type="text/javascript">
function cdtd() {
var xmas = new Date("' . $factory_date . '");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
clearTimeout(timer);
$("#factory_upgrade").load("/include/factory_upgraded.php");
$("#quantidade_fabricas").load("/include/factory_stats.php");
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;' .
"var tempo=('0' + hours).slice(-2)+':'+('0' + minutes).slice(-2)+':'+('0' + seconds).slice(-2);"
.
'
document.getElementById("secsBox").innerHTML = tempo;
var timer = setTimeout("cdtd()",1000);
}
</script>
';
echo '<div class="success_upgrades">';
echo '<div class="upgrade_text"><b>Nivel: </b>' . $factory_level . '</div><div class="div_separator"></div><div class="upgrade_text"><b>Duração:</b>
<div class="secsBox" id="secsBox"></div>
<script type="text/javascript">cdtd();</script></div><div class="div_separator"></div>
';
echo '<div id="close" class="stop_upgrade" ></img></div>';
echo '</div>';
echo '<script>
$(".stop_upgrade").click(function (e) {
e.preventDefault();
$(factory_upgrade2).empty();
setTimeout(function(){
$("#factory_upgrade2").load("/include/upgrade_cancel.php");
$("#factory_upgrade").load("/include/factory_update.php");
$("#load").load("/include/cabecalho_content.php");
$("#industrial").fadeIn();
$(loader1).delay(1000).hide(0);
}, 100);
});
</script>
';
}
}
The format of $factory_date is invalid according to standard JavaScript and Chrome happens to be able to parse it.
For better results, stick to these Date constructors:
new Date();
new Date(value);
new Date(dateString);
new Date(year, month [, day, hour, minute, second, millisecond]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
I have this code for a countdown timer. its basically a combination of PHP/Javascript countdown timer which will gets the $end_date from a Mysql table/field.
The problem is that it will stop automatically (which is unwanted) at a certain time.
For example: I set the $end_date to September 19 2013 11:30:00 AM GMT in mysql database.
the countdown starts and works fine and starts counting down as it should. However, when the countdown timer reaches September 19 2013 13:00:00 PM GMT it will stop and it will show the Times Up message! Basically it will stop working or counting down once the $end_date has been changed to 13:00:00 PM.
I cannot see anything in my code that will cause this issue. apart from this line:
if ($now < $exp_date ) {
?>
but again, this line only tells the script when to start counting and as far i can see it shouldn't stop the countdown timer to stop as long as the timer has not reached the $end_date. or am I missing something?
here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php date_default_timezone_set('GMT'); ?>
<?php
session_start();
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "config/connect.php";
$dynamicList = "";
$sql = "SELECT * FROM item ORDER BY id";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$date_added = date("Y-m-d", strtotime($row["date_added"]));
$end_date = date("F d Y H:i:s A T", strtotime($row["end_date"]));
$price = $row["price"];
$dynamicList .= '<div>' . $end_date . '
</div>';
}
} else {
$dynamicList = "No Records";
}
?>
<?php
$date = $end_date;
$exp_date = strtotime($date);
$now = time();
if ($now < $exp_date ) {
?>
<script>
// Count down milliseconds = server_end - server_now = client_end - client_now
var server_end = <?php echo $exp_date; ?> * 1000;
var server_now = <?php echo time(); ?> * 1000;
var client_now = new Date().getTime();
var end = server_end - server_now + client_now; // this is the real end time
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour *24
var timer;
function showRemaining()
{
var now = new Date();
var distance = end - now;
if (distance < 0 ) {
clearInterval( timer );
document.getElementById('countdown').innerHTML = 'EXPIRED!';
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor( (distance % _day ) / _hour );
var minutes = Math.floor( (distance % _hour) / _minute );
var seconds = Math.floor( (distance % _minute) / _second );
var countdown = document.getElementById('countdown');
countdown.innerHTML = '';
if (days) {
countdown.innerHTML += 'Days: ' + days + '<br />';
}
countdown.innerHTML += 'Hours: ' + hours+ '<br />';
countdown.innerHTML += 'Minutes: ' + minutes+ '<br />';
countdown.innerHTML += 'Seconds: ' + seconds+ '<br />';
}
timer = setInterval(showRemaining, 1000);
</script>
<?php
} else {
echo "Times Up";
}
?>
<div id="countdown"></div>
any help would be greatly appreciated.
13:00:00 PM is not a valid time. AM and PM are used to indicate which side of the 12-hour cycle the time is. You cannot logically say you're on the 13th hour of the 12 hour side of the clock.
EDIT: For clarity: 13:00 == 1 PM, 13:00 PM == nothing.