PHP - Setting Date To 1st Monday Of Month
i have this code to set a date to the 1st of the previous month --
SELECT DATE_FORMAT(DATE_SUB(tbl_col, INTERVAL 1 MONTH), '%Y-%m-01') FROM table can it be changed to set date to first Monday in that month...? Similar TutorialsHello. I'm new to pHp and I would like to know how to get my $date_posted to read as March 12, 2012, instead of 2012-12-03. Here is the code: Code: [Select] <?php $sql = " SELECT id, title, date_posted, summary FROM blog_posts ORDER BY date_posted ASC LIMIT 10 "; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $title = $row['title']; $date_posted = $row['date_posted']; $summary = $row['summary']; echo "<h3>$title</h3>\n"; echo "<p>$date_posted</p>\n"; echo "<p>$summary</p>\n"; echo "<p><a href=\"post.php?id=$id\" title=\"Read More\">Read More...</a></p>\n"; } ?> I have tried the date() function but it always updates with the current time & date so I'm a little confused on how I get this to work. I am looking for a way to have my 'start_date', echo ('start_date' + 1 month). I am looking at the line that reads .... date("F Y",strtotime($row['start_date'])). Does anyone have a suggestion. The date format read, July 2014 or alphabetical full month, numerical full year. Thank you in advance.
<?php $query = sqlsrv_query($conn, $sql);if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)){ echo "<tr> <td><a href='view_invoice.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>$row[meter_id]</a></td> <td>" . date("F Y",strtotime($row['start_date'])) . "<td>$row[invoice_no]</td><td>" . date_format($row['invoice_date'],'Y-m-d') . "<td>$row[amount_paid]</td>" . "<td>$row[payment_date]</td>" . "<td>$row[check_number]</td>" ;}sqlsrv_free_stmt($query); ?> Edited by Butterbean, 11 January 2015 - 08:19 PM. Hi if i have $date = "12-08-2018" or "2018-08-12" Need to subtract a month. Could this be done for day and year even? TIA Desmond. P.S. What I want to do is clear out a database table with records over a month old so today is 27th Jan 2018. I would like to create a date “2018-12-01” I can then use this in an SQL to delete all before “2018-12-01” Edited January 27, 2019 by Paul-Ddate in my database is save as (dd/mm/yyyy). now i want to create sql to search for activity for chosen month..how do i extract month from (dd/mm/yyyy) and include in sql? Hello, I've been struggling for days on this problem now. And I'm getting frustrated! I know that the answer is simple, and I've done it before but I cannot figure out what I'm doing wrong. What do I want? Well, I get a date from the database in this format: YYYY-MM-DD. I first want to distract one month from that and then set in into this form: YYYY, MM, DD. I've got this but it's not working (I've tried a lot different values). $transformdate = date("Y-m-d", strtotime("-1 month", $d_datevalue)); $startpoint = str_replace("-", ", ", $transformdate); Thanks, NLCJ Hi, I am having set of records, id name dob 1 philip 1278226800 2 winsent 1278216800 3 Benn 1272226800 now i want to fetch the records based on the month and listing out, can anyone help me to write for the above concept. Thanks, Hi guys, i was having an issue with dates and although it leans a bit more into MYSQL I was hoping i could get some help ive searched all over the net for this so I dont know if im not thinking on how to ask this correctly or can't find it, but ill be as detailed as possible and hope that helps.
Ok I have a table that shows when payment was received on past invoices dating back to say 1995; The company processes 'ALL' invoices every month on the 9th, so if anything comes in from the first to the ninth it will be processed that month on the ninth. If it comes in from the tenth to the thirtieth or thirty-first it will be processed the following month.
The table already has a "Received" date filed and now I want to create a column on the table called "Processed Date" (adding the column is no problem don't need help with that part) that shows me the date we processed the invoice.
I know i have to use the "Received" field (because it shows what day the invoice was put into the system) but im not sure how to format the "Processed Date" field, so say the "Received" field shows an invoice came in on '1995-05-11', I want the "Processed Date" field to display it was payed on '1995-06-09' (notice that is the following month).
I thought i would put in a visual of what I was trying to do below:
So if I pull the two columns in MYSQL from that table I would see something like this:
Received Processed Date
1996-02-03 1996-02-09
1996-03-03 1996-03-09
1996-04-11 1996-05-09
1996-05-20 1996-06-09
1996-07-13 1996-08-09
If i could get some guidance/help on this i would really appreciate it.
thanks in advance!!
JL
Hi,
I'm new to php,
I'm trying to get the month value from a date stamp (ie I have a variable with the following sort of value: 2014-08-20 07:32:20 )
how do I create a new variable with just the month values (ie 08) of the date stamp??
any help would be appreciated.
Thanks !
Hi, I'm having trouble with this: Code: [Select] $date = date('d/m/y') ; echo $date; echo "<br/>"; $date1 = strtotime('date -1 month'); echo date('Y-m-d', $date1); My result looks like this: Current Date: 19/05/11 Date minus 1 month: 1970-01-01 I want it to look like this: Current Date: 19/05/11 Date minus 1 month: 2011-04-19 Hi Guys, Been a while since I've been on here, but I'm currently bashing my head against a wall trying to figure this one out. Basically I have a calendar that outputs Monday through Sunday, and the user clicks on a date and outputs it to the URL.... ie. ?day=$day&month=$month&year=$year. Now the next part I need to find out what day (0-6) say the 25th Feb 2011 will be wday 5... now how can I do this in code? And before anyone says why not output it to url, I cannot as the first week and last weeks use different code Cheers James I have a php page where i need to list all item in a particular month.But the format for date stored is MMMM DD,yyyy that January 22, 2011. How do i extract the month name only from the above format Hi, I've got a date picker on a form which puts data into a database in the YYYY-MM-DD format. Just wondering how I could also put the name of the month (extracted from that) as well as just the year into separate columns. Ie: To use the field race_date from the form to also fill the 'race_month' and 'race_year' columns in the database. This code obviously only fills the 'race_date' column so far: Code: [Select] global $_POST; $race_date = $_POST["race_date"] ; .... $query = "INSERT INTO 10k_races (race_date, race_month, race_year)" . "VALUES ( '$race_date', '$race_month', '$race_year')"; Hello, I have a datepicker that when a date is selected, the post to a php file will get the sum values for that selected day, month, and year. I have the "by day" working with this... <?php $choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['choice'])) : date("Y-m-d"); $con = mysql_connect("localhost","root","xxxxxxxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("inverters", $con); $sql = "SELECT sum(power/1000) AS choice FROM feed WHERE date = '".$choice."' group by date"; $res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error()); $row = mysql_fetch_assoc($res); echo $row['choice'].'<br />'; ?> But by month and year are not clicking... Help Please Alan I am using the following code to get the third sunday of every month for +6 Months from todays date. I have put together the following code which works perfectly when the date is echoed however when it comes to inserting those dates into mysql db it throws a unable to convert to string error. Im new to this and my head is in a spin as to why those dates can't be inserted if they can echoed ok. I have spent 2 days at it now and searched many forum however they seem to get side tracked from my issue (or do they)??????? I figure I can't go mixing DateTime and date() the way I have? Please help??? Code: [Select] [php] $month=date('F'); $now=date('Y-m-d'); $year=date('Y'); $num='3'; //example only this will be a $_POST value 0-3 $day='Sunday'; //example only this will be a $_POST value Monday-Sunday $start= date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.'')); if($start>$now) { $begin=strtotime('+'.$num.' week '.$day.' '.$month.' '.$year.''); } else { $d = new DateTime( $start ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $year = $d->format( 'Y' ); $begin=strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.''); } $date=date('Y-m-d', $begin); # insert into db first occurance //mysql_select_db($database); //$query_rsFirst = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsFirst = mysql_query($query_rsFirst) or die(mysql_error()); echo $date.'<br/>'; for($i = 0; $i <= 4; $i++) { $d = new DateTime( $date ); $d->modify( 'first day of next month' ); $nextmonth = $d->format( 'F' ); $date=date('Y-m-d', strtotime('+'.$num.' week '.$day.' '.$nextmonth.' '.$year.'')); # insert into db //mysql_select_db($database); //$query_rsDate = "INSERT INTO event_date VALUES (NULL, '$event', '$d', '$date', '$date')"; //$rsDate = mysql_query($query_rsDate) or die(mysql_error()); echo $date.'<br/>'; if($nextmonth=="December") { $d = new DateTime( $date ); $d->modify( 'first day of next year' ); $year = $d->format( 'Y' ); } } [/php] I have set up a function to validate user input date in yyyy-mm-dd format taking into account the following possibilities:
the length of the month
February 29th when the year is not a leap year
When I run the code, I do not get any feedback whatsoever, when it should.
This is the code below and I would appreciate your thoughts:
if(!function_exists('checkdate')) { function checkdate($date, $checkyear, $currentmonth) { if($checkyear == 0)//if it is not a leap year { // if current month is february if($currentmonth == 2) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-8])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } // if current months are april, june, september or november elseif($currentmonth == 4 || $currentmonth == 6 || $currentmonth == 9 || $currentmonth ==11) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-8]|3[0])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } else { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-8]|3[01])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } } elseif($checkyear == 1)//if it is a leap year { // if current month is february if($currentmonth == 2) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-9])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } // if current months are april, june, september or november elseif($currentmonth == 4 || $currentmonth == 6 || $currentmonth == 9 || $currentmonth ==11) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-9]|3[0])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } else { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } } } $todate = getdate(); $currentmonth = $todate['mon']; $checkyear = date('L'); $date = 2014-02-31; echo checkdate($date, $checkyear, $currentmonth); Edited by terungwa, 26 July 2014 - 02:43 PM. Im using the script to let users login in to our site from 9.00 am to 6.00 pm from Monday To friday, timing is working fine but the day part don't, any ideas why, here is the top of the script is on the login.php page Code: [Select] <? $hour = date('H');//hour 1-24 $dayofweek = date('N');//numeric day of week 1=mon, 7=sun if($hour < 6 || $hour > 15 || $dayofweek > 5) { header( "Location:Messages.php?msg=12" ); }else { //login code session_start(); if(isset($_GET['reg'])){ $reg=$_GET['reg']; }else{ $reg=""; } if($reg==1){ $msg1="<font color=\"#FF6600\">Your details have been added, please login</font>"; }elseif($reg==2){ $msg1="<font color=\"#FF6600\">You have been successfully logged out.</font>"; } if(isset($_POST['submit'])){ if( empty($_POST['uname']) && (empty($_POST['upass']))){ header( "Location:Messages.php?msg=1" ); exit(); } //transfer to shorter var $n=$_POST['uname']; $p=$_POST['upass']; //connect to db include('config.php'); $query="SELECT * FROM user WHERE uname = '".$_POST['uname']."' AND pw = PASSWORD('".$_POST[upass]."')"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num>0 ){ //put in session vars $mytime=time(); $mytime=date("H:i:s A",$mytime); $_SESSION['time'] = $mytime; $_SESSION['status'] = 'logged'; $_SESSION['username'] = $n; //goto next page header("location:Welcome.php"); exit; }else{ $_SESSION['status'] = 'not logged'; header( "Location:Messages.php?msg=2" ); exit(); } } } ?> Ok well i have a virtual world, my friendcoded the emulator. In PHP but he's on holiday for 2 weeks. I keep getting Quote PHP Warning: PHP Startup: It is not safe to rely on the system's timezone setti ngs. You are *required* to use the date.timezone setting or the date_default_tim ezone_set() function. In case you used any of those methods and you are still ge tting this warning, you most likely misspelled the timezone identifier. We selec ted 'Europe/London' for '0.0/no DST' instead in Unknown on line 0 PHP Warning: eval(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone _set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ' Europe/London' for '0.0/no DST' instead in C:\inetpub\wwwroot\main\server\chapat iz.php(2) : eval()'d code on line 4 But there are only 4 lines in chapatiz.php and it has nothing to do with it i think Quote <?php eval(file_get_contents('serv_original.ini')); ?> Here's chapatiz.php (Its attached since its big) Quote //config $socketurl = "munizonline.co.uk; $httpurl = "munizonine.co.uk; $rootPath = "c:/inetpub/wwwroot/"; // SQL SETTINGS $host = 'localhost'; $user = 'root'; $dbname = 'chapatiz'; $password = '---'; $sqli = new mysqli("p:". $host, $user, $password, $dbname); // create a persistant sql connexion (keepAlive) $banned_ips = array(); ///// ADD BAN BY IP FROM HTACCESS $filename = $rootPath.".htaccess"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $contents = utf8_decode($contents); $ex = explode("\n", $contents); $exf = array(); foreach ($ex AS $k => $str) { if(preg_match('`deny from (.*?) \#`', $str, $sortie)) { $return = $sortie[1]; array_push($exf, $return); } } foreach ($exf AS $k => $str) { array_push($banned_ips, $str); } // LOG ALL ACTIONS function dolog($text) { $myFile = "c:/inetpub/wwwroot/main/log/chat.txt"; $fh = fopen($myFile, 'a'); fwrite($fh, $text. "\n"); fclose($fh); } function serve($r) { return file_get_contents('C:/inetpub/wwwroot/main/server/serve/'. $r. '.php'); } function getitems($name) { return file_get_contents('http://munizonline.co.uk/main/items.php?u='. $name); } function haveitem($name, $item) { $c = file_get_contents('http://munizonline.co.uk/main/items.php?u='. $name); $pattern = '/"'. $item .'"/'; $pattern2 = '/"0'. $item .'"/'; if ((preg_match($pattern, $c)) OR (preg_match($pattern2, $c))) { return true; } else { return false; } } function dotag($tag) { $tag = explode('##', $tag); $tags = array(); foreach ($tag AS $k => $v) { list($name, $value) = explode('=', $v); $tags[$name] = $value; } return $tags; } function updatetag($tags, $tochange, $value) { $tags[$tochange] = $value; $new = "##"; foreach ($tags AS $k => $v) { $new .= $k. "=". $v. '##'; } return $new; } ini_set('display_errors', '1'); error_reporting(0); $quete8 = $sqli->query('UPDATE phpbb_users SET online = 0'); function cmd() { return false; } $modee = false; session_start(); $_SESSION['start'] = "yess"; $mainsock = socket_create(AF_INET, SOCK_STREAM, 0); //modifiez la prochaine ligne si besoin $pport = rand(81, 8000); while (!socket_bind($mainsock, $socketurl, $pport)) { $pport = rand(81, 8000); echo "\n port modified to ". $pport. "\n"; } $myFile = $rootPath."main/server/socket.php"; $fh = fopen($myFile, 'w+') or die("can't open file"); fwrite($fh, $socketurl. ":". $pport); fclose($fh); socket_set_nonblock($mainsock); socket_listen($mainsock); $clients=Array(); $compteur=0; $write = NULL; $except = NULL; $lol = NULL; $race = "0"; $flag = "0"; date_default_timezone_set('Europe/London'); $ok_marry = "0"; $ok_pv = "0"; function random($car) { $string = ""; return $string; } function random3($car3) { $string3 = ""; return $string3; } function random2($car2) { $string2 = ""; return $string2; } function random1($car1) { $string1 = ""; return $string1; } echo("Waiting for players!\n"); $file = file_get_contents($rootPath.'main/server/server_while.php'); $lasteval = time(); while(true){ if ($lasteval <= strtotime('-1 minute')) { if (file_exists($rootPath."main/server/server_while.php")) { $file = file_get_contents($rootPath.'main/server/server_while.php'); } $lasteval = time(); echo "\neval again\n\n"; } eval($file); } I have no clue whats wrong it use to work. Hi I'm having a problem getting a query to work. I have a simple form with user input for start and end date with format: 2009-03-19 (todays date): $Startdate = $_POST['date']; This works well when something is entered into the form, and afterwards using my query: SELECT COUNT(*) as total FROM mydb WHERE Date BETWEEN '$Startdate' AND '$EndDate' ........ Problem is if user submits the form without entering anything in the date input fields, which makes sense. I want to check if inputs has been made, and if not set af default date, but can't make it work: if (isset($_POST['date']) && $_POST['date'] !='') { $Startdate = $_POST['date'];} else { $Startdate = '1980-01-01';} How can I set $Startdate to something that can be used in the query as below doesn't work? So I am creating a list of events. There will be a drop down menu to change between months. I only want the months to be listed if an event during that month exists. So if I have events listed for September and November, list only September and November, skipping October. Any idea on how to approach this or if there are any functions for such a thing? Trying to get the below to generate the month in word form from a numeric entry in a database. Code: [Select] elseif($_GET['rma']=="calander"){ $sql101010="SELECT DISTINCT rma_year_issued FROM $tbl_name4 ORDER BY rma_year_issued"; $result101010=mysql_query($sql101010); while($row101010=mysql_fetch_array($result101010)){ extract($row101010); $content.='<a href="./acp_admincp.php?rma=calander&year='.$rma_year_issued.'">'.$rma_year_issued.'</a> <br />'; } if(isset($_GET['year'])){ $logout.=' | <a href="./acp_admincp.php?rma=calander">Back to RMA Calander</a>'; $rma_year_issued=$_GET['year']; $sql111010="SELECT DISTINCT rma_month_issued FROM $tbl_name4 WHERE rma_year_issued='$rma_year_issued' ORDER BY rma_month_issued"; $result111010=mysql_query($sql111010); while($row111010=mysql_fetch_array($result111010)){ extract($row111010); $months = array('Janurary', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); for($i=0; $i <=11; $i++){ if($rma_month_issued=$i){ $rma_month_issued2=$months[$i]; } } $content=""; $content.='<a href="./acp_admincp.php?rma=calander&year='.$rma_year_issued.'&month='.$rma_month_issued.'">'.$rma_month_issued2.'</a> <br />'; } } } Basically in the data base I may have 2,5,7 under the year 2011. I want to get the numeric months into words for use in the text of the link. The above is returning December even though the entry in the database is 5. |