How can I echo out the selected date - php

does anyone know how I would echo out the selected date as text with the date month and year separated outside of the form? I tried echoing out $date $month and $year outside of the form however this doesn't give me the correct date thankyou for the help
<?
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28',
'16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');
$currentdate = date('y-m-d');
echo $currentdate;
?>
<form>
<select style="width:200px;">
<?php
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
$selected = "selected";
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
echo 'the current billing period is';
}
?>
</select>
</form>

Inside of your loop add a $selected_int variable like so:
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
$selected = "selected";
$selected_int = $i;
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
echo 'the current billing period is';
}
Then, you can reference it like:
echo date('Y-m-d', strtotime($date[$selected_int]));
Addition
I know you've already accepted the answer, but I also wanted to make a suggestion now that I see what you are using the $date for. Since you know the start date, and it is in 14-day periods, it would be easy to write that as part of the loop.
$start_date = date('Y-m-d', strtotime(date('Y').'-01-01'); //First day of the year, for the sake of argument.
$interval = 14;
for ($i = 0; date('Y') == date('Y', strtotime($start_date.' +'.($i * $interval).' days')); $i++) {//While this year is equal to the start date's year with the added interval [If I knew what your logic here was I could offer a better suggestion]
if ($currentdate >= date("Y-m-d", strtotime($start_date.' +'.($i * $interval).' days')) && (date('Y') < date("Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')) || $currentdate < date("m/d/Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')))) {
$selected = "selected";
$selected_int = $i;
} else {
$selected = "";
}
echo "<option $selected>" . date("m/d/Y", strtotime($start_date.' +'.($i * $interval).' days')) . "</option>";
}
Basically, this takes the start date, shows it as the first date option, then adds 14 days to it with each pass through. Your if/else statement should still be the same. It checks to see if you are on the last interval of the year, or if the current date is less than the next interval, and also that the current date is greater than the current interval.
After your loop, you can get the date by:
echo date("m/d/Y", strtotime($start_date.' +'.($selected_int * $interval).' days'));
I know it seems like a lot, but it would save you from having to make a date array to begin with.

Use strtotime instead list.
....
// list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
....
EDIT: Additional information - your code requires a lot modification and likely some structure changes but assuming this is for testing a method and "how to do" instead a final product.
You need to submit the selected date, catch it in the script and use the selected date to do what you need - i.e. retrieve data from database - and this should give you some idea.
<?php
// You need to create these dates by using another method. You cannot hard code these. You can create it with date functions easily.
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28','16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28','16-11-14','16-11-28','16-12-14','16-12-28');
// Checking if we have a posted form, with the button name user clicked
if (isset($_POST["btnSubmit"])) {
// This is your selected day - use it where you need:
$selectedDate = $_POST["selectedDate"];
// This is where your model start singing and gets necessary info for this date - just printing here as sample
print $selectedDate;
// I need dropDownDate to compare in the SELECT to preselect the appropriate date
$dropDownDate = strtotime($selectedDate);
} else {
// First time visit, preselect the nearest date by using current date
$dropDownDate = time();
}
?>
<form method="post">
<select name="selectedDate" style="width:200px;">
<?php
foreach ($date as $i => $d) {
if ($dropDownDate >= strtotime($d) &&
(!isset($date[$i+1]) || ($dropDownDate < strtotime($date[$i+1])))
) {
$selected = 'selected="selected"';
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
}
?>
</select>
<input type="submit" name="btnSubmit" value="Submit">
</form>
Note that I added a "submit" type input (to submit the form) and changed form method to "post", finally named SELECT as "selectedDate". I also changed your date comparison code line in the loop.
Hope this helps.

Related

Working dates between two given dates in Php

Please, i need assistance in this code.I have checked others in Stakeoverflow, but it is not combatible, hence this question. I want to generate all working /weekdays between two dates.I have found a code, but it is generating all days, including weekend. How do i eliminate the weekend from the list or ensure the list generated is ONLY for weekdays?
<?php
$start_Date = date('Y-m-d');
$end_Date = date('Y-m-d', strtotime('30 weekdays'));
//echo $start_Date."<br/>";
//echo $end_Date."<br/>";
// Specify the start date. This date can be any English textual format
$date_from = $start_Date;
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = $end_Date;
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp
// Loop from the start date to end date and output all dates inbetween
$c = 0;
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
I expect 30days to be generated but with this code, I am getting 42days . Weekend has been added,instead of weekdays ONLY .
Just add this to your loop:
$w = date('w',$i);// day of week - Sunday == 0, Saturday == 6
if($w == 0 || $w == 6){
continue;
}
DEMO
Your code is almost working only have to add a if checking in your code
your code
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
please replace with that one
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$day = date("w", $i);
if($day != 0 && $day!= 6){ // will continue if not Sunday or Saturday
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
}
You also can take help from php.net
Thanks
You may need to get the day of the week, like date("D"), then use it in your for loop to check..something like this?:
$Weekends = array("Sat","Sun");
for....
$DayOfWeek = date("D",$i);
if(!in_array($DayOfWeek, $Weekend)){
// increment...
}

Select dropdown from date range

Is there a simple if statement to default select the correct date from the dropdown? so for example if the date is 02/11/16 then it will default select
01/28/16
Basically what I'm asking for is an if statement that default selects the drop-down that the date is within
<?php
date_default_timezone_set('US/Eastern');
$currenttime = date('d:m:y:h:i:s A');
list($day,$month,$year) = split(':',$currenttime);
$currentdate = "$month/$day/$year";
?>
<form>
<select>
<option>01/14/16</option>
<option>01/28/16</option>
<option>02/14/16</option>
///the list goes on for ages so i saved time and cropped out the rest of the dates.
</select>
</form>
Put all the dates in an array, and loop through them. Then you can test them and decide whether the current date is in the billing period.
$dates = array('16-01-14',
'16-01-28',
'16-02-14',
...);
$currentdate = date('y-m-d');
?>
<form>
<select>
<?php
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($dates)-1 || $currentdate < $dates[$i+1])) {
$selected = "selected";
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>$month/$day/$year</option>";
}
?>
</select>
</form>
I changed the format of $currentdate to y-m-d so that they can be compared as strings to see if the date is in a range.
As it loops through the list of dates, it tests whether the current date is between that date and the next date in the array (or it's the last date in the array). If it is, it adds the selected attribute to the <option>.

Increment date when it should be a new month

I have a PHP script which records things based on the day. So it will have a weekly set of inputs you would enter.
I get the data correctly, but when i do $day ++; it will increment the day, going passed the end of the month without ticking the month.
example:
//12/29
//12/30
//12/31
//12/32
//12/33
Where it should look like
//12/29
//12/30
//12/31
//01/01
//01/02
My script is as follows:
$week = date ("Y-m-d", strtotime("last sunday"));
$day = $week;
$run = array(7); //this is actually defined in the data posted to the script, which is pretty much just getting the value of the array index for the query string.
foreach( $run as $key=>$value)
{
$num = $key + 1;
$items[] = "($num, $user, $value, 'run', '$day')";
echo "".$day;
$day ++;
}
Should I be manipulating the datetime differently for day incrementations?
You can use
$day = date("Y-m-d", strtotime($day . " +1 day"));
instead of
$day++;
See live demo in ideone
You refer to $day as a "datetime" but it is just a string - that is what date() returns. So when you do $day++ you are adding 1 to "2015-12-02". PHP will do everything it can to make "2015-12-02" into a number and then add 1 to it, which is not date math. Here is a simple example:
<?php
$name = "Fallenreaper1";
$name++;
echo $name
?>
This will output:
Fallenreaper2
This is how I would do it, using an appropriate data type (DateTime):
<?php
$day = new DateTime('last sunday');
$run = array(7);
foreach ($run as $key => $value) {
$num = $key + 1;
$dayStr = $day->format('Y-m-d');
$items[] = "($num, $user, $value, 'run', '$dayStr')";
echo $dayStr;
$day->modify('+1 day');
}
To increase time you should use strtotime("+1 day");
here is simple example of using it
<?php
$now_time = time();
for($i=1;$i<8;$i++) {
$now_time = strtotime("+1 day", $now_time);
echo date("Y-m-d", $now_time) . "<br>";
}
?>

Compare Dates PHP

I want to only shows result for the current month, but I have no idea how to do this, my current output is like this.
Output
2014-04-11
-> array
2014-04-11
2014-04-05
2014-03-29
PHP
$date = date('Y-m-d');
echo $date;
echo "<pre>";
foreach ($submissions as $test){
if($date >= substr($test['thing']['created'], 0, 10)){
echo substr($test['thing']['created'], 0, 10);
echo "<br>";
}
}
echo "</pre>";
My current code wont work as its only checking if the whole number is greater or equal to, any ideas anyone?
Just try with strtotime:
$year = date('Y');
$month = date('m');
foreach ($submissions as $test) {
$timestamp = strtotime($test['thing']['created']);
$testYear = date('Y', $timestamp);
$testMonth = date('m', $timestamp);
if (($month >= $testMonth && $year == $testYear) || $year > $testYear) {
// test passed
}
}
If your problem is to have only results of current month :
$date = date('Y-m');
echo "<pre>";
foreach ($submissions as $test){
// if month & year is equal
if($date == substr($test['thing']['created'], 0, 7)){
echo substr($test['thing']['created'], 0, 10);
echo "<br>";
}
}
echo "</pre>";
If you want to sort it, you will have to convert to timestamp
i'm not a pro in php but take a look here : http://fr2.php.net/manual/fr/function.date.php
does date('m') would be a solution for your problem?
You can try 2 ways :
convert them to timestamp, then compare the int
If you are taking the dates frome a DB, ORDER BY date in the query
Variable for current month first $cur_m=date('m')
Explode your variable in loop with -.
$p = explode('-',$test['thing']['created']);
in loop check for current month like below
if($p[1]==$cur_m){ your code... }

Getting the next date of a day in MySQL database (PHP)

I have a series of weekly events in a database along with the day they happen on (in full form, so: 'Monday', 'Tuesday' etc). I've successfully printed the events in a while loop ordered by today, tomorrow, etc, but I'd like to put the date in brackets next to each one.
I thought it might be a case of (mock code):
$today = date("l");
$todays_date = date("j M");
if (day == $today) {
$date = $todays_date;
}
else if (day == $today + 1) {
$date = $todays_date + 1;
}
else if (day == $today + 2) {
$date = $todays_date + 2;
}
etc...
But I'm not so sure. It'd be ideal if I could just have the date in the database, but this seems to go against the grain of what MySQL is about.
Also, I'd like to ideally format the date as: 11 Jun.
EDIT
Presumably it's also got to fit into my while loop somehow:
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array($row[0]);
echo "<option>" . $row[0] . "</option>";
}
}
You can use strtotime?
echo "Today: ".date("j M");
echo "Tomorrow: ".date("j M", strotime("+1 day"));
You can use strtotime:
echo strtotime("+1 day");

Categories