Convert PHP-Date in Javascript-Date - javascript

With this PHP-code I get the first day of the actual month
date('Y, n, 1')
The output ist
2014, 4, 1
How can I convert it to the Javascript-Date because the Javascript-Date, especially the month is zerobased. That means the output 2014, 4, 1 will be the 1st may 2014.

Try this:
<?php
$month = date("n");
$year = date("Y");
$day = 1;
?>
var myDate=new Date("<?php echo $month?> <?php echo $day?>, <?php echo $year?>");
alert(myDate);
May be this help. JS Date Demo

Related

How to print fullcalendar with given date range including all events?

How can someone print fullcalendar or export fullcalendar in PDF or Excel format?
For example:
Input:
Start Date: 01 Jan 2020
End Date: 31 Dec 2020
On click of button it will get all of the events between given date ranges and populate it to the fullcalendar
There will be something like...
26th January 2020 which will have an event - Australia Day
14th February 2020 which will have an event - Valentines day
...
...
25th December 2020 which will have an event - X-Mas Day
As you can see there will be multiple events in different months and what I am looking for is printing or exporting this fullcalendar in a single page with all 12 number of months.
Your response will help many.
You can use the below sample code by editing , hope it helps
<?php
header("Content-Type: text/html; charset=iso-8859-9");
header("Content-Type: text/html; charset=windows-1254");
header("Content-Type: text/html; charset=x-mac-turkish");
$today = date("d-m-Y");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=$today-BOOK.xls");
$date = '2020-05-01';
//get end date of month
$end = '2020-05-' . date('t', strtotime($date));
include("../../DATABASE.php");
?>
<table>
<tr>
<td>PERSONAL</td>
<?php
while(strtotime($date) <= strtotime($end)){
$day_num = date('d', strtotime($date));
$day_name = date('l', strtotime($date));
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
echo "<td>$day_num <br/> $day_name</td>";
}
?>
</tr>
<?php
//get end date of month
$hsql=mysql_query("select * from XXXUSER");
while($hyaz=mysql_fetch_array($hsql)){
$firstname=$hyaz["firstname"];
$kullanicidi=$hyaz["id"];
echo "<tr>";
echo "<td>$firstname</td>";
$fromdate = '2020-06-01';
$todate = '2020-06-' . date('t', strtotime($fromdate));
while(strtotime($fromdate) <= strtotime($todate)) {
$geneltarih = date('Y-m-d', strtotime($fromdate));
$fromdate = date("Y-m-d", strtotime("+1 day",strtotime($fromdate)));
$xxxsql = mysql_fetch_array(mysql_query("select * from XXXSCHEDULE where kaynakcodu='$kullanicidi' and start_event='$geneltarih'"));
$title = $xxxsql["title"];
echo " <td>$title</td>";
}
echo "</tr>";
}
?>
</table>

Bootstrap year calendar offset events

I'm trying to display some reservations from the database into bootstrap year calendar but each reservation is offset with +1 month.
For example: 28 Nov, 2019 - 01 Dec, 2019 will be displayed like: 28 Dec, 2019 - 01 Jan, 2020
The only solution will be to add -1 month but in this case reservations from Dec-Jan will jump with one year and the 2020 will be unusable.
Did anyone meet this situation ?
.ready(function(){
var currentYear = new Date().getFullYear();
jQuery('.calendar')
.calendar({
style : 'background',
alwaysHalfDay : true,
enableContextMenu : true,
enableRangeSelection: true,
dataSource: [
<?php
$bookings = $method->has_get_booking_of_room($room_id);
foreach($bookings as $row) : //break;
?>
{
booking_id: '<?php echo $row["booking_id"];?>',
startDate : new Date(<?php echo date("Y", $row['checkin_timestamp']);?>, <?php echo date("n", $row['checkin_timestamp']);?>, <?php echo date("j", $row['checkin_timestamp']);?>),
endDate : new Date(<?php echo date("Y", $row['checkout_timestamp']);?>, <?php echo date("n", $row['checkout_timestamp']);?>, <?php echo date("j", $row['checkout_timestamp']);?>)
},
<?php
endforeach;
?>
]
});
});

can you manage how to add a day in a date in php?

<?php
$outerarray = array();
$myeventsmodels=Events::model()->findAll();
foreach($myeventsmodels as $myeventsmodel{
$innerarray=array();
$stop_date=date('Y-m-d H:i:s',strtotime($stop_date.'+1 day'));
$innerarray['title']=$myeventsmodel->title;
$innerarray['start']=$myeventsmodel($date)->start_date;
$innerarray['end']=$myeventsmodel->end_date;
$innerarray['url']='/uobportal/#/event';
$outerarray[]=$innerarray;
}
$events=json_encode($outerarray);
?>
You can try like this :
$stop_date = new DateTime('2009-02-15 10:21:00');
echo 'date before day adding: ' . $stop_date->format('Y-m-d H:i:s');
$stop_date->modify('+1 day');
echo 'date after adding 1 day: ' . $stop_date->format('Y-m-d H:i:s');
You can use date_add function here the example:
$stop_date = "2013-03-15";
date_add($stop_date ,date_interval_create_from_date_string("5 days"));
echo date_format($stop_date,"Y-m-d");
the output will be:
2013-03-20
Reference https://www.w3schools.com/php/func_date_date_add.asp
or create a new Date object with existing date plus the number of days you want to add like this:
$stop_date = "2010-09-17";
echo date('Y-m-d', strtotime($stop_date. ' + 1 days'));
echo date('Y-m-d', strtotime($stop_date. ' + 2 days'));
Output:
2010-09-18
2010-09-19

get jQuery variable in php from one page to another

I have a loop on my archive-tour page in which I output dates of on week after then the selected date.
<ul class="uol">
<?php
$start = strtotime($_GET['date']);
$dates = array();
for ($i = 0; $i <= 7; $i++) {
$date = date('Y-m-d', strtotime("+$i day", $start));
$date1 = $date;
$day = date('D', strtotime($date1));
$date = explode('-', $date);
$dateinput = date('Y-m-d', strtotime("+$i day", $start));
$date = $date[2];
echo '<li class="lia"><input type="hidden" class="getdate" value="'.$dateinput.'">' . $date . ' ' . $day . '</li>';
}
?>
<li class="lia" style="background-color:blue"><a style="background- color:blue" href="#tabs-0" class="date tdate"></a></li>
</ul>
Here i have another loop on buttons as seen below.
1:00 am - 2:00 am......
<button type="button" style="background-color:blue" id="ttime<?php echo $post->ID?>" class="btn btn-default time-pick tt_time" value=""></button>
for ($i = 0; $i <= 23; $i++) {
am = "";
if ($i <= 12) {
$am = "am";
} else {
$am = "pm";
}
echo '<button type="button" class="btn btn-default">' . $i.':00 ' . $am . ' - ' . ++$i .':00 '. $am . '</button> ';
--$i;
}
Now user can select date and according time to book tour.
i am getting the selected time and date and i want to highlight the above date and time accordingly. currently setting time and date outer then the loop. but i want to match date with selected date, if it present then highlight it and same for the time. for example in user selects 9:00 am - 10:00 am then my code add one more button with this time. i want to highlight the existing when by applying a if condition in loop that if date=selcted_date then highlight that one.
jQuery('#time<?php echo $post->ID?>').html(t_time);
$(".tdate").html(date);
how can i send the jquery variable t_time from footer page to archive page and get it on archive page in php so that i can use it in if condition.
Remember its is wordpress.
In jquery you can use localStorage.
localStorage.setItem('t_time','Your setTime' ); //set your value
And to get
localStorage.getItem('t_time'); //Get your value
Use jquery's $.post() as follows..
var ttime = jQuery('#time<?php echo $post->ID?>').html();
$.post('php_file_name.php',{'ttime':ttime},function(data,status){
});
And in your php file..
$time = $_POST['ttime'];//get ttime send from jquery
//echo $time

how to convert strtotime in php to new Date in js

For a realtime countdown i use some php variables and echo them in the js like below:
$expire_year = '2016'; // year
$expire_month = '8'; // month ( 8 = August)
$expire_day = '14'; // day (14th)
$expire_hour = '2'; // hour ( 1-24)
$expire_minutes = '12'; // minutes
i use them in the js like blow:
timestamp = new Date(<?php echo $expire_year; ?>, <?php echo $expire_month - 1; ?>, <?php echo $expire_day; ?>, <?php echo $expire_hour; ?>, <?php echo $expire_minutes; ?>),
This works fine!
I use php strtotime to check if the date really has expired:
$quickpollexpiredate = strtotime("August 14, 2016 2:12"); // set an expiration date
So for the countdown timer in js and for the check in php, i have to use 2 different date "structures".
Is it possible to make a check in php so that i have to use only the 5 variables ($expire_year, $expire_month...) with strtotime() or another function?
So actually what i want to achieve is something like this (ofcourse this is wrong, but i hope you understand what i am trying to achieve)
$quickpollexpiredate = strtotime($expire_year, $expire_month, $expire_day, $expire_hour, $expire_minutes); // set an expiration date
If you have a timestamp in php, you can easily use it to set a Date object in javascript. You just need to consider that a timestamp in javascript is in milliseconds and not seconds like in php:
jsTimestamp = new Date(<?php echo $phpTimestamp; ?> * 1000);
or
jsTimestamp = new Date(<?php echo strtotime($yourDateString); ?> * 1000);
Note that new Date(...) also accepts strings, see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date for more details.

Categories

Resources