I currently have this method:
function fill_date_jaarlijks()
{
var today = new Date();
$("#datepicker_eind").datepicker("setDate", new Date(today.getFullYear()+1,today.getMonth(),today.getDay()));
}
What it should do is, return the date of (today + 1 year).
What it actually returns is 01 December 2012, while today's date is 19 December 2011.
Could someone explain?
getDay returns day of week
Use getDate instead.
Or better still:
var dateValue = new Date();
dateValue.setFullYear(dateValue.getFullYear() + 1);
$("#datepicker_eind").datepicker("setDate", dateValue);
Related
I need to convert 'Nov22' into a date object that is in the month of November. I am trying the following - but it only works with months with 31 days:
$novDateString = 'Nov22';
$decDateString = 'Dec22';
$novDate = DateTime::createFromFormat('My', $novDateString);
$decDate = DateTime::createFromFormat('My', $decDateString);
echo $novDate->format('m');
echo $decDate->format('m');
// output
12
12
As you can see, both Nov22 and Dec22 go to December. In fact, all months with less than 31 days go map to the month ahead of it. Is this a known issue or is there an easy way to solve?
Since you're not specifying a day, it's defaulting to today, which is the 31st:
$novDateString = 'Nov22';
$decDateString = 'Dec22';
$novDate = DateTime::createFromFormat('My', $novDateString);
// DateTime #1669926271 {#4573
// date: 2022-12-01 15:24:31.0 America/New_York (-05:00),
// }
$decDate = DateTime::createFromFormat('My', $decDateString);
//DateTime #1672518273 {#4578
// date: 2022-12-31 15:24:33.0 America/New_York (-05:00),
// }
November 31st doesn't exist, so the day becomes December 1st. You need to prepend a day before the string, and then pass in the correct format:
$novDate = DateTime::createFromFormat('jMy', '1'.$novDateString);
//DateTime #1667331673 {#4576
// date: 2022-11-01 15:41:13.0 America/New_York (-04:00),
// }
I know this question has been asked many times as I have found a few on google and also on stackoverflow.
but none of them explained how to format my datetime in my php so it works in combination with jquery countdown timer. so I am going to ask it here in a hope i get someone shed a light on this for me.
Basically what i am trying to do is to create a countdown timer which will work with mysql datetime.
the datetime is stored in mysql so All need to do is to get the correct format in my php so the countdown timer could work with it.
I am using this plugin: http://keith-wood.name/countdown.html
and here is what i have so far:
PHP formatting:
$end_date = date("m d Y H:i:s T", strtotime($row["end_date"]));
Jquery/Javascript code:
<script type="text/javascript">
$(function(){
var countdown = $('#countdown'),
ts = new Date(<?php echo $end_date * 1000; ?>),
finished = true;
if((new Date()) > ts)
{
finished = false;
}
$('#defaultCountdown').countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds)
{
var message = "";
message += days + " days, ";
message += hours + " hours, ";
message += minutes + " minutes, ";
message += seconds + " seconds ";
message = (finished ? "Countdown finished" : "left untill the New Year");
countdown.html(message);
}
});
});
</script>
when i run this code, all i get is 0 hours, 0 minutes, 0 seconds.
I can only suspect that the issue is from formatting the datetime in my php section!
or am i missing something else as well?
okay I have managed to minify the code to this:
<script type="text/javascript">
$(document).ready(function () {
$('#defaultCountdown').countdown({
until: new Date(<?php echo $end_date; ?>),
compact: true
});
});
</script>
and changed the php to this:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
However, the time shown in the coutdown timer is wrong (way off).
the $end_date is: September 22 2013 23:30:00 GMT in mysql datetime
but the jquery countdown timer is showing:
34d 06:21:48
2013, 9, 22, 23, 30, 00
34days and 6 hours blah blah is absolutely wrong!
what am i doing wrong here now?
The JavaScript Date object is constructed as follows:
Date(year, month, day, hours, minutes, seconds, milliseconds)
That means you probably should be doing something along these lines:
$end_date = date("Y, n, j, G, i, s", strtotime($row["end_date"]));
Sources:
JavaScript Date-object
PHP date-function
EDIT:
In addition, I seem to have found the problem in the jQuery Countdown manual:
A note on Date - the JavaScript Date constructor expects the year,
month, and day as parameters. However, the month ranges from 0 to 11.
To make explicit what date is intended (does a month of 3 mean March
or April?) I specify the month from 1 to 12 and manually subtract the
1. Thus the following denotes 25 December, 2010.
So, you'd have to split the string, substract 1 from the month and rebuild...
$tmp_date = explode(', ', $end_date);
$tmp_date[1] = $tmp_date[1] - 1;
$end_date = implode(', ', $tmp_date);
Link to jsFiddle
I need to implement something that checks if a given date is greater than today. If for example, I input a date of April 19, 2011 while today is April 15, 2011, there should be some validator/pop up error. How do I implement this?
I have my system date (today's date) working fine through php. I just don't know how to create a validation/error message when the user inputs a higher date than today.
It can be done with PHP (on server side) and with JavaScript (on client side, in browser).
Here is an example of how to do it on JavaScript:
var currentTime = new Date()
month = currentTime.getMonth(),
day = currentTime.getDate(),
year = currentTime.getFullYear(),
today = year + "-" + month + "-" + day;
var users_day = '2011-04-19';
if (users_day > today) {
alert ("Entered day is greater than today");
}
else {
alert ("Today is greater than entered day");
}
For example (in PHP)
date_default_timezone_set(date_default_timezone_get()); // not necessary here
$today = strtotime('2011-04-15');
$users_day = strtotime('2011-04-19');
if ($users_day > $today) {
echo "Error";
}
else {
echo "OK";
}
Example above outputs
Error
...because April 19, 2011 (user's input) is greater than April 15th, 2011 (today).
I create a timestamp with strtotime php function. My question is how to "reverse" timestamp to year, month, date, ... seconds etc using javascript ?
var date = new Date(phptimestamp*1000);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
//etc...
var date = new Date(seconds*1000);
where milliseconds is seconds since Jan 1970.
var date = new Date(timestamp*1000);
I need to create functions in PHP that let me step up/down given datetime units. Specifically, I need to be able to move to the next/previous month from the current one.
I thought I could do this using DateTime::add/sub(P1M). However, when trying to get the previous month, it messes up if the date value = 31- looks like it's actually trying to count back 30 days instead of decrementing the month value!:
$prevMonth = new DateTime('2010-12-31');
Try to decrement the month:
$prevMonth->sub(new DateInterval('P1M')); // = '2010-12-01'
$prevMonth->add(DateInterval::createFromDateString('-1 month')); // = '2010-12-01'
$prevMonth->sub(DateInterval::createFromDateString('+1 month')); // = '2010-12-01'
$prevMonth->add(DateInterval::createFromDateString('previous month')); // = '2010-12-01'
This certainly seems like the wrong behavior. Anyone have any insight?
Thanks-
NOTE: PHP version 5.3.3
(Credit actually belongs to Alex for pointing this out in the comments)
The problem is not a PHP one but a GNU one, as outlined here:
Relative items in date strings
The key here is differentiating between the concept of 'this date last month', which, because months are 'fuzzy units' with different numbers of dates, is impossible to define for a date like Dec 31 (because Nov 31 doesn't exist), and the concept of 'last month, irrespective of date'.
If all we're interested in is the previous month, the only way to gaurantee a proper DateInterval calculation is to reset the date value to the 1st, or some other number that every month will have.
What really strikes me is how undocumented this issue is, in PHP and elsewhere- considering how much date-dependent software it's probably affecting.
Here's a safe way to handle it:
/*
Handles month/year increment calculations in a safe way,
avoiding the pitfall of 'fuzzy' month units.
Returns a DateTime object with incremented month/year values, and a date value == 1.
*/
function incrementDate($startDate, $monthIncrement = 0, $yearIncrement = 0) {
$startingTimeStamp = $startDate->getTimestamp();
// Get the month value of the given date:
$monthString = date('Y-m', $startingTimeStamp);
// Create a date string corresponding to the 1st of the give month,
// making it safe for monthly/yearly calculations:
$safeDateString = "first day of $monthString";
// Increment date by given month/year increments:
$incrementedDateString = "$safeDateString $monthIncrement month $yearIncrement year";
$newTimeStamp = strtotime($incrementedDateString);
$newDate = DateTime::createFromFormat('U', $newTimeStamp);
return $newDate;
}
Easiest way to achieve this in my opinion is using mktime.
Like this:
$date = mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo date('d-m-Y', $date);
Greetz Michael
p.s mktime documentation can be found here: http://nl2.php.net/mktime
You could go old school on it and just use the date and strtotime functions.
$date = '2010-12-31';
$monthOnly = date('Y-m', strtotime($date));
$previousMonth = date('Y-m-d', strtotime($monthOnly . ' -1 month'));
(This maybe should be a comment but it's to long for one)
Here is how it works on windows 7 Apache 2.2.15 with PHP 5.3.3:
<?php $dt = new DateTime('2010-12-31');
$dt->sub(new DateInterval('P1M'));
print $dt->format('Y-m-d').'<br>';
$dt->add(DateInterval::createFromDateString('-1 month'));
print $dt->format('Y-m-d').'<br>';
$dt->sub(DateInterval::createFromDateString('+1 month'));
print $dt->format('Y-m-d').'<br>';
$dt->add(DateInterval::createFromDateString('previous month'));
print $dt->format('Y-m-d').'<br>'; ?>
2010-12-01
2010-11-01
2010-10-01
2010-09-01
So this does seem to confirm it's related to the GNU above.
Note: IMO the code below works as expected.
$dt->sub(new DateInterval('P1M'));
Current month: 12
Last month: 11
Number of Days in 12th month: 31
Number of Days in 11th month: 30
Dec 31st - 31 days = Nov 31st
Nov 31st = Nov 1 + 31 Days = 1st of Dec (30+1)