strtotime question - php

This should be a simple fix;
I currently have a calendar for selecting a range of date:
http://protekco.com/index.php/en/reservations/1718-carvelle-drive-westpalm-beach.html
When the dates are selected in the calendar, it populates 2 input fields for Check in and Check out dates. The problem is, the calendar initially was set 1 day late, so Thursday Sept 22 actually showed as a Thursday Sept 21. I was able to just echo a +1 on the day count, but the input boxes still show the erroneous date. Essentially, I'm trying to add the same +1, but because the date is returned as yyyy/mm/dd, +1 doesn't do much.
Here is the code:
$listsFrom = $this->lists['from'];
$selectedFrom = $listsFrom ? strtotime($listsFrom) : 0;
$listsTo = $this->lists['to'];
$selectedTo = $listsTo ? strtotime($listsTo) : $selectedFrom;
if ($selectedTo) {
ADocument::addDomreadyEvent('Calendars.checkOut = ' . $selectedTo . ';');
}
if ($selectedFrom) {
ADocument::addDomreadyEvent('Calendars.checkIn = ' . $selectedFrom . ';');
}
$listOperation = $this->lists['operation'];
ADocument::addDomreadyEvent('Calendars.operation = ' . $listOperation . ';');
Any ideas?
Thank you!
Edit:
<td class="<?php echo implode(' ', $class); ?>" <?php if ($canReserveBox) { ?> id="day<?php echo $firstDay->Uts+84600000; ?>" onclick="Calendars.setCheckDate('day<?php echo $firstDay->Uts+84600000; ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_MYSQL_DATE, 0); ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NORMAL); ?>')"<?php } ?> >
<span class="date" >
<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NICE_SHORT)+1;
?>
</span>
This is what actually calls the changes. The + 84600000 is my addition, it doesn't seem to do much tho.

If the date is in yyyy/mm/dd format, first use strtotime() to convert it to a timestamp, and then you can use it again to add one day.
$date = '2011/06/01';
$ts = strtotime('+24 hours', strtotime($date));
$date = date('Y/m/d', $ts);
That is just one possible solution.

Related

how to get 4-days ago to now - php

If current date is 2016-03-06, I would like to get these dates :
2016-03-06
2016-03-05
2016-03-04
2016-03-03
I'm trying to get this purpose but my result not what I want :
$_4date = date("y-m-d",strtotime("day"));
$_3date = date("y-m-d",strtotime("-1 day"));
$_2date = date("y-m-d",strtotime("-2 day"));
$_1date = date("y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
the result is :
70-01-01
16-03-05
16-03-04
16-03-03
To get today's date with strtotime, you do strtotime("today");. However, as Bjorn has commented, you can simply just call date() directly.
Furthermore, the reason you are not getting the year in four digits is because you are using a lowercase y instead of an uppercase Y.
Try date("Y-m-d", strtotime("-1 day"));.
The following piece of code illustrates the required changes:
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
echo "$today <br />";
echo "$yesterday <br />";
// Output
2016-03-06
2016-03-05
For more informtation, please consult the PHP documentation on the date function. It actually shows you that what to expect from y and Y and it also shows you that the default value that is passed as the second argument is time(), meaning the default is the current time.
PHP's strtotime documentation can be consulted for more information on the strtotime() function and its possible parameters.
Always check the (PHP) documentation first before asking a question.
You need to use like that:
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
Explanation:
For current date no need to use use strtotime().
For full year you need to use this format Y-m-d.
y-m-d will return you the date 16-03-06 but Y-m-d will return you 2016-03-06.
Use a for loop with strtotime( "... days ago" ):
for( $i = 0; $i < 4; $i++ )
{
echo date( 'Y-m-d', strtotime( "$i days ago" ) ) . PHP_EOL;
}
The first loop (0 days ago) will output today date, other loops will output past days.
3v4l.org demo
You need to use capital 'Y' for full year (2016) instead of small 'y' which will display year in shorthand (16).
And for current date just use date("Y-m-d").
<?php
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
?>
Working Example
<?php
$date = [date("Y-m-d")];
for($i = 1; $i < 4; $i++) {
$date[] = date("Y-m-d",strtotime("-$i day"));
}
//For cli output you'll need:
echo implode("\n", $date) . "\n";
//For web output you'll need:
echo implode("<br />", $date) . "<br />";

How to compare digits with date PHP

Is there a way to convert values to date? I have 3 dropdown lists: day, month and year. If the selected date < today, then the consumers won't be able to click on the buy button til the day i selected in de dropdown.
The problem is, that the date I selected, is a value and not a date. Is there a way to compare my value with the date of today? The code below is the backend of my website, the if statement has to stay on the frontend
$releasedatumdag= get_post_meta( $domeinnaam_extensies->ID, 'releasedatumdag', true );
$releasedatummaand= get_post_meta( $domeinnaam_extensies->ID, 'releasedatummaand', true );
$releasedatumjaar= get_post_meta( $domeinnaam_extensies->ID, 'releasedatumjaar', true );
<tr>
<th>Releasedatum:</th>
<td>
<select name="domeinnaam_extensies_releasedatumdag">
<?php
for($idag = 1; $idag <= 31; $idag++){
echo '<option value="'.$idag.'">'.$idag.'</option>';
}
?>
</select>
<select name="domeinnaam_extensies_releasedatummaand">
<?php
for($imaand = 1; $imaand <= 12; $imaand++){
echo '<option value="'.$imaand.'">'.$imaand.'</option>';
}
?>
</select>
<select name="domeinnaam_extensies_releasedatumjaar">
<?php
for($ijaar = 2000; $ijaar < date("Y")+10; $ijaar++){
echo '<option value="'.$ijaar.'">'.$ijaar.'</option>';
}
?>
</select>
</td>
</tr>
You can use DateTime() to create your date. You then can compare it to another DateTime object representing today since DateTime objects are comparable:
$my_date = DateTime::createFromFormat('d/m/Y', '31/12/2014');
$now = new DateTime();
if ($my_date < $now) {
// do something
}
You have a day, month, and a year so just create a DateTime object and you can get the timestamp for it and compare it to today.
http://www.php.net/manual/en/datetime.construct.php
http://php.net/manual/en/function.checkdate.php
Can string the digits into any format that you want
$dateString = $releasedatumdag . '/' . $releasedatummaand . '/' . $releasedatumjaar;
//Make sure that they didn't try to create Feb. 31
if(checkdate($dateString)) {
$submittedTime = new DateTime($dateString);
$today = new DateTime();
//DO YOUR COMPARISON HERE
}

I have Week and year in php I need to convert date complete

I have Week and year in php I need to convert date complete, I'm new in php help please
<?php
$weekY = date('Wy' ,strtotime('-1 week'));
print_r(date("Y-m-d",strtotime($weekY)));
?>
the result is 1970-01-01
and I have the 5113(Wy) in my database how to convert in Wy
<?php
$weekY = date('Wy' ,strtotime(5113));
echo $weekY."<br/>";
?>
the result is 0170
I read examples but do not help me in my problem
Make use of setISODate
<?php
$gendate = new DateTime();
$gendate->setISODate(2013,52);//year and week
echo $gendate->format('d-m-Y'); //"prints" 23-12-2013
thanks for everything, and I have the answer to my question
<?php
$y = substr('5213',-2);
$Y = 20..$y;
$W = substr('5213',0,2);
echo "Year + Week: ".$Y." ".$W."<br/>";
$date = new DateTime();
$date->setISODate($Y, $W);
echo "Result: ".$date->format('Y-m-d') . "\n";
?>

Get the number of days till today for the current month and current year

How To get the number of days that have already passed from the current month and the number of days that have already passed from the current year eg if today is April 6.
I should be getting 6 for current month and
I should be getting 97 for current year
Is there a way I can get the same excluding SUNDAYS
Take a look at the php date() function. It has a parameter 'z', which is the day of the year, in your situation 97:
echo date('z');
Same with day of the month:
echo date('j');
You need to use getdate function
var_dump( getdate() );
Will output:
array(11) {
...
["mday"]=>
int(6)
...
["yday"]=>
int(96)
...
}
PHP has excellent documentation about this:
http://php.net/manual/en/function.date.php
The current month in php can be retrieved with date("n") and the current day of the year is date("z") (starting with zero).
$dayNumber = date("z") + 1;
date("z") starts counting from 0
this will surely help you
<?php
$dt = new DateTime;
if(isset($_GET['year']) && isset($_GET['week'])) {
$dt->
setISODate($_GET['year'], $_GET['week']);}
else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
}
$year = $dt->format('o');
$week = $dt->format('W');
?>
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week- 1) .
'&year='.$year; ? >">Pre Week</a>
<a href="<?php echo $_SERVER['PHP_SELF'].'?week='.
($week+1).'&year='.$year;
? >">Next Week</a>
<table>
<tr>
<td>Employee</td>
<?php
do{
echo "<td>" . $dt->format('l') . "<br>" . $dt->format('d M Y') . "
</td>\n";
$dt->modify('+1 day');
}while ($week == $dt->format('W'));
?>

How to show numerical date in mm/dd/yy format

Right now i am the month, day, and year of a user seperated in three db fields titled month, day, year.
When i display it i am doing:
$month = $row['month'];
$day = $row['day'];
$year = $row['year'];
then to echo it:
$month/$day/$year
The problem is that the PHP is doing mathematics here and dividing the numbers... What can i do to not make that happen and let it simply display the dates..
Thanks
try this out :
echo "{$month}/{$day}/{$year}";
echo $month.'/'.$day.'/'.$year;
date('m/d/Y',strtotime($month . ' ' . $day . ' ' . $year));
The advantage of this is that you can choose how to format the date independent of how it is stored in your database: http://php.net/manual/en/function.date.php
echo $month . "/" . $day . "/" . $year;
Doing string concatenation.
or
echo "{$month}/{$day}/{$year}";
Doing string interpolation.
See the difference/performance of the two here.

Categories