Hey guys take a look over this code. This code is showing current month stats,but if i want to check last month stats instead of this month then what changes i need to do in this small code ?
$date_time_array = getdate(time());
$last_month = $date_time_array["month"];
$this_year = $date_time_array["year"];
$last_mon = $date_time_array["mon"];
$date = mktime(0,0,0,$last_mon,1,$this_year); //The get's the first of March 2009
$links = array();
$newstamp = time() - 3600;
$cpc = 0;
$cpm = 0;
$click = 0;
for($n=1;$n <= date('t',$date);$n++){
$thisdate = $this_year.'-'.$last_mon.'-'.str_pad($n, 2, '0', STR_PAD_LEFT );
$sql = "select * from pub_st_daily where user='$user' && STR_TO_DATE(pdate,'%Y-%m-%d') = '$thisdate' ";
You can do this :
$date = new DateTime("07/30/2015"); //Will give you July 30th
$date_time_array=getdate($date->format("U"));
...
Demo
Related
I try to insert data in my custom table
$month = date('m');
$year = date('y');
$number = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for ($i = 0; $i < $number; $i++) {
$test = new Test();
$test->cuid = $user->id;
$test->name = $user->name;
$test->month = date('m');
$test->created_at = '2018-04-'+$i;
$test->save();
}
i try to insert number record in a month
let's say April have 30 days i try to insert it working fine but in created_at field i need insert 2018-04-01 to 2018-04-30 but this is insert only current date so that i try above but not working
you need to set timestamps = false;
$test->timestamps = false
I'm trying to use one's date of birth to calculate when he'll be 50, if he's not 50 already.
If person is not 50, add a year to his age then check if it'll be 50. If not, iterate until it's true. Then get the date he turned 50. in PHP
Here's the code, not complete.
$rAge = 50;
$retir = date('j F Y ', strtotime("+30 days"));
$oneMonthAdded = strtotime(date("d-m-Y", strtotime($DOB)). "+1 year");
$re = date("d-m-Y", $oneMonthAdded);
$futDate = date("d-m-Y", strtotime(date("d-m-Y", strtotime($re))));
$date_diff = strtotime($futDate)-strtotime($DOB);
$future_age = floor(($date_diff)/(60*60*24*365));
Help please.
try this code bro!
// your date of birth
$dateOfBirth = '1950-11-26';
// date when he'll turn 50
$dateToFifty = date('Y-m-d', strtotime($dateOfBirth . '+50 Years'));
// current date
$currentDate = date('Y-m-d');
$result = 'retired';
// checks if already fifty
if($currentDate <= $dateToFifty) {
$result = $dateToFifty;
}
echo $result;
I use simplest php code to find out retire date. y
<?php
$dob = '1970-02-01';
$dob_ex = explode("-",$dob);
$age_diff = date_diff(date_create($dob), date_create('today'))->y;
$year_of_retire = 50 - $age_diff;
$end = date('Y', strtotime('+'.$year_of_retire.'years'));
$date_of_retire = $end."-".$dob_ex[1]."-".$dob_ex[2];
echo $date_of_retire;
?>
you can use if...else condition to echo values according to you.
like
if($year_of_retire > 0){
echo $date_of_retire;
} else if($year_of_retire < 0){
echo "retired";
}
Starting with the number 9 and using php, I would like to be able to count up from there, and echo out the next number in increments of 1.
So, number 9, then after 1 month the number would change to 10, then another month 11, then 12 etc., with no maximum number/stop point.
How can I accomplish this? So far I have the below code.
$number = 9;
$output = $number + 1;
echo $output;
Is there a way to set this to increase once a month?
You can do this with the PHP date()-function. This is one example of doing it if you are not dependent on the day of the month, but adding day functionality is possible and should be quit easy.
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
From your question, I'd say:
$number = 9;
$output = date('n') + $number;
echo $output;
But that depends on what you are trying to accomplish. You can also wrap the number around the date() with a modulo.
However this is nothing random. If you want to create a random number every month like your topic suggests, use the month as the random seed.
srand(date('n'));
$number = rand();
a very inefficient way would be
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
this script would have to be run continuously for months.
A better way would be
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
But this script would increase it whenever called as the first open date would be stored. Will work forever (till UNIX can timestamp).
for this purpose you have to store the number in the database, compare with current unix timestamp and update it when the new month is reached.
2 database columns: count_month int(10) and next_month int(10) where next_month will contain the unix timestamp of the first day of the next month. you can run it with cronjobs or on production.
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>
Hi firstly heres my code.
<?php
function getDatesBetween2Dates($startTime, $endTime) {
$day = 86400;
$format = 'd-m-Y';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$numDays = round(($endTime - $startTime) / $day) + 1;
$days = array();
for ($i = 0; $i < $numDays; $i++) {
$days[] = date($format, ($startTime + ($i * $day)));
}
return $days;
}
///
$days = getDatesBetween2Dates(date('d-m-Y', strtotime('-3 weeks Monday')),date('d-m-Y', strtotime('+2 weeks Sunday')));
foreach($days as $key => $value){
$dayNumber = date('d', strtotime($value));
//echo $value;
echo "<div id=\"day\">
<div id=\"number\">$dayNumber</div>";
////////////sql seearch//\\\/////////
//Connect to db
include("../djwbt.php");
$sql = "SELECT * FROM daysummary WHERE date='$value'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$place = $row['place'];
$invoicedate = $row['date'];
}
/////////////end sql search//////////
echo "<div id=\"event\">$place</div>
</div><!-- end day -->";
}
?>
What i am trying to do is show all dates between two points and for each of the dates search my db using the date as a where clause. i have tried putting the search in a few places but im not getting the right results.
this gives me the same result in each date.
e.g. 17th = (empty) as in my db, 18TH = HOME (as in my db), 19th = HOME (not as in my db), 20th = HOME (this continues all the way through fore each)
the link in each fore each works perfectly?
Any help would be amazing.
I would make one statement that gets all the needed data from your database:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
then use the foreach loop for the results
Note that mysql_ functions are deprecated, Try switching to mysqli_ or PDO
I have the day and the month from the database, stored in seperate variables. But if today is the 7th and I minus one week, it would be 0 same case for the month. And I want to add the lo_date to lockin_period_date and minus 3 months, as I want it to send an email to me 3 months in advance. How do I set the values I have from the database to the date I want? Please help.
<?php
include"connect_mysql.php";
$reminder_dates = mysql_query("SELECT*FROM registration_form");
while($row = mysql_fetch_array($reminder_dates)){
$main_dob_day = $row['main_dob_day'];
$main_dob_month = $row['main_dob_month'];
$main_dob_year = $row['main_dob_year'];
$joint_dob_day = $row['joint_dob_day'];
$joint_dob_month = $row['joint_dob_month'];
$joint_dob_year = $row['joint_dob_year'];
$loan_lo_day = $row['loan_lo_day'];
$loan_lo_month = $row['loan_lo_month'];
$loan_lo_year = $row['loan_lo_year'];
$lockin_period_day = $row['lockin_period_day'];
$lockin_period_month = $row['lockin_period_month'];
$lockin_period_year = $row['lockin_period_year'];
$legal_fee_clawback_day = $row['legal_fee_clawback_day'];
$legal_fee_clawback_month = $row['legal_fee_clawback_month'];
$legal_fee_clawback_year = $row['legal_fee_clawback_year'];
date_default_timezone_set('UTC');
$m = date("n");
$d = date("j");
$y = date("Y");
$time = time();
$today = date('n-j-Y', $time);
//main applicant birthday - reminder 7days in advance
$main_day = $main_dob_day-7;
if($main_day == $d && $main_dob_month == $m){
echo "Mail Sent!";
}
//
}
?>
A working solution would be:
$main_day = date( 'n-j-Y', strtotime( "today -1 week"));
To apply it to your use-case:
$time = mktime( 0, 0, 0, $main_dob_month, $main_dob_day, $main_dob_year);
$main_day = date( 'n-j-Y', strtotime( "-1 week", $time)); echo $main_day;
$oneWeekAdv=$main_dob_year.'-'.$main_dob_month.'-'.$main_dob_day;
$oneWeekAdv=strtotime(date("y-m-d", strtotime($oneWeekAdv)) . " -1 week");
$oneWeekAdv=date('m-d', $oneWeekAdv);