javascript subtracting date numbers from each other - php

I'm passing a date in this format; YYYY-MM-DD as a php variable to a javascript function.
however when i get it in the js function - its gone ahead and subtracted the numbers
//php
$today = "2013-05-30";
onclick="myjsfunction($today)"
//js
function myjsfunction(today)
{
alert(today);
}
this give 1978 (2013 minus 5 minus 30) !
anyone know a way round this?
thanks

Try this :
$today = "\"2013-05-30\"";
Or
$today = "'2013-05-30'";
Or, even better:
//php
$today = "2013-05-30";
onclick="myjsfunction('$today')"
The idea is that the result of echo (onclick); have to be myjsfunction('2013-05-30') or myjsfunction("2013-05-30") for the parameter to be handled like a string insted of an integer (result of 2013 minus 5 minus 30).

Related

Is there a function to calculate weekdays from today PHP

I'm writing a code which need to calculate the number of weekdays from a date to today.
I just have today's date and with that i want to pass a number to the function so it can return me back the number of weekdays since x days.
e.g :
function getWorkingDays($number){
// code...
return $value;
}
// if we are monday
getWorkingDays(2) // return Thursday's date
I got this problem since two days now and i'm getting very boring, hope someone got a solution.
This is very easy to do with the DateTime object:
$date = date_create('2020-01-06'); //a Monday
$numberWeekdays = 2;
$date->modify('-'.$numberWeekdays.' weekdays');
echo $date->format('l Y-m-d');
//Thursday 2020-01-02
If you need Today as the basis , you can also use date_create('Today').

PHP - Adding lunar month to date in PHP, carolan window

I tried to calculate date in php based on this formula:
<?PHP
$lunarMonth = 29.530589;
function getFib($n)
{
return round(pow((sqrt(5)+1)/2, $n) / sqrt(5));
}
$fibbonaciNumber = getFib(2);
$addToDate = sqrt($fibbonaciNumber*$lunarMonth);
$date = '2017-01-01';
$timeStamp = strtotime($date);
$calucatedDate = $timeStamp+$addToDate;
echo 'Date: '.date('Y-m-d', $calucatedDate).'<br>';
?>
But it dosen't work, i cant calculate new date by using timeStamp function. What should i try?
Sample output is:
addToDate: 24.902657870195
timeStemp: 1483311600
CalculateDate: 1483311624.9027
Date: 2017-01-02
This show always the same date, no matter what CalculateDate date is.
I current using excel to calculate this:
=DATE(YEAR(A4)+0#MONTH(A4)+0#DAY(A4)+(29,530589*(SQRT(1)))),
for example input: 2009-02-18, should output: 2009-03-18, for fibonacci number 1
I need to move base date based on calucation $fibbonaciNumber*$lunarMonth
Thanks

php strtotime() values not working as expected

this code keeps telling me that $lasUpdate is always greater than $yesterday no matter the change i make to $yesterday result is (12/31/14 is greater than 01/19/15 no update needed). i feel like i'm missing something simple thank you in advance it is greatly appreciated.
$result['MAX(Date)']='12/31/14';
$lastUpdate = date('m/d/y', strtotime($result['MAX(Date)']));
$yesterday = date('m/d/y', strtotime('-1 day'));
if($lastUpdate<$yesterday){echo $lastUpdate.'is less '.$yesterday.'<br>'.'update needed';}
if($lastUpdate>=$yesterday){echo $lastUpdate.'is greater than '.$yesterday.'<br>'.'no update needed';
You have fallen victim to PHP type juggling with strings. A date function has a return value of a string. You cannot compare dates in their string format since PHP will juggle strings into integers in the context of a comparison. The only exception is if the string is a valid number. In essence, you are doing:
if ('12/31/14' < '01/19/15') { ... }
if ('12/31/14' >= '01/19/15') { ... }
Which PHP type juggles to:
if (12 < 1) { ... }
if (12 >= 1) { ... }
And returns false on the first instance, and true on the second instance.
Your solution is to not wrap date around the strtotime functions, and just use the returned timestamps from the strtotime functions themselves to compare UNIX timestamps directly:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
You will however want to use date when you do the echo back to the user so they have a meaningful date string to work with.
Try something like this:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');
if ($lastUpdate < $yesterday) { /* do Something */ }
12/31/14 is greater than 01/19/15
Because 1 is greater than 0. If you want to compare dates that way you will need to store them in a different format (from most to least significant digit), for example Ymd.
Or store the timestamps you are making in the different variables and compare them.

PHP convert 2 digit year to a 4 digit year

I have data coming from the database in a 2 digit year format 13 I am looking to convert this to 2013 I tried the following code below...
$result = '13';
$year = date("Y", strtotime($result));
But it returned 1969
How can I fix this?
$dt = DateTime::createFromFormat('y', '13');
echo $dt->format('Y'); // output: 2013
69 will result in 2069. 70 will result in 1970. If you're ok with such a rule then leave as is, otherwise, prepend your own century data according to your own rule.
One important piece of information you haven't included is: how do you think a 2-digit year should be converted to a 4-digit year?
For example, I'm guessing you believe 01/01/13 is in 2013. What about 01/01/23? Is that 2023? Or 1923? Or even 1623?
Most implementations will choose a 100-year period and assume the 2-digits refer to a year within that period.
Simplest example: year is in range 2000-2099.
// $shortyear is guaranteed to be in range 00-99
$year = 2000 + $shortyear;
What if we want a different range?
$baseyear = 1963; // range is 1963-2062
// this is, of course, years of Doctor Who!
$shortyear = 81;
$year = 100 + $baseyear + ($shortyear - $baseyear) % 100;
Try it out. This uses the modulo function (the bit with %) to calculate the offset from your base year.
$result = '13';
$year = '20'.$result;
if($year > date('Y')) {
$year = $year - 100;
}
//80 will be changed to 1980
//12 -> 2012
Use the DateTime class, especially DateTime::createFromFormat(), for this:
$result = '13';
// parsing the year as year in YY format
$dt = DateTime::createFromFormat('y', $result);
// echo it in YYYY format
echo $dt->format('Y');
The issue is with strtotime. Try the same thing with strtotime("now").
Simply prepend (add to the front) the string "20" manually:
$result = '13';
$year = "20".$result;
echo $year; //returns 2013
This might be dumbest, but a quick fix would be:
$result = '13';
$result = '1/1/20' . $result;
$year = date("Y", strtotime($result)); // Returns 2013
Or you can use something like this:
date_create_from_format('y', $result);
You can create a date object given a format with date_create_from_format()
http://www.php.net/manual/en/datetime.createfromformat.php
$year = date_create_from_format('y', $result);
echo $year->format('Y')
I'm just a newbie hack and I know this code is quite long. I stumbled across your question when I was looking for a solution to my problem. I'm entering data into an HTML form (too lazy to type the 4 digit year) and then writing to a DB and I (for reasons I won't bore you with) want to store the date in a 4 digit year format. Just the reverse of your issue.
The form returns $date (I know I shouldn't use that word but I did) as 01/01/01. I determine the current year ($yn) and compare it. No matter what year entered is if the date is this century it will become 20XX. But if it's less than 100 (this century) like 89 it will come out 1989. And it will continue to work in the future as the year changes. Always good for 100 years. Hope this helps you.
// break $date into two strings
$datebegin = substr($date, 0,6);
$dateend = substr($date, 6,2);
// get last two digits of current year
$yn=date("y");
// determine century
if ($dateend > $yn && $dateend < 100)
{
$year2=19;
}
elseif ($dateend <= $yn)
{
$year2=20;
}
// bring both strings back into one
$date = $datebegin . $year2 . $dateend;
I had similar issues importing excel (CSV) DOB fields, with antiquated n.american style date format with 2 digit year. I needed to write proper yyyy-mm-dd to the db. while not perfect, this is what I did:
//$col contains the old date stamp with 2 digit year such as 2/10/66 or 5/18/00
$yr = \DateTime::createFromFormat('m/d/y', $col)->format('Y');
if ($yr > date('Y')) $yr = $yr - 100;
$md = \DateTime::createFromFormat('m/d/y', $col)->format('m-d');
$col = $yr . "-" . $md;
//$col now contains a new date stamp, 1966-2-10, or 2000-5-18 resp.
If you are certain the year is always 20 something then the first answer works, otherwise, there is really no way to do what is being asked period. You have no idea if the year is past, current or future century.
Granted, there is not enough information in the question to determine if these dates are always <= now, but even then, you would not know if 01 was 1901 or 2001. Its just not possible.
None of us will live past 2099, so you can effectively use this piece of code for 77 years.
This will print 19-10-2022 instead of 19-10-22.
$date1 = date('d-m-20y h:i:s');

Year comparison in PHP

if($_POST['syear']){
$compy = strtotime($_POST['syear']);
if(date("Y") <= date("Y", $compy)){
//success
$startdate = $_POST['syear'];
}
else{
$error = 6;
}
}
I have created the above code and have no idea where I have gone wrong. I am posting a string from a form with a number in it and want to compare it to the current year. If the number is equal to or less than the current year it is supposed to be a success. It is always a success even if the number is larger than the current year. Do I need to convert some strings to ints or have I missed something entirely.
PHP handle string comparison very well, did you try this directly ? (and changing the comparison order to >=)
if($_POST['syear']){
if(date("Y") >= $_POST['syear']){
$startdate = $_POST['syear'];
}else{
$error = 6;
}
}
You cannot convert simply a year to time. Using your example as is, you need to have a string of yyyy/mm/dd format to use strtotime. If you are really just checking year, you can use January 1st as a check date.
$compy = strtotime($_POST['syear'] . '-01-01' );
You can compare integers or strings the same way. If you want to be sure about the type comparison you can always cast the variable but in this case it's pointless.
2012 is inferior to 2013. "2012" is inferior to "2013". date( 'Y' ) returns a 4 characters string you can compare with your $_POST['syear'] if it's a 4 character string. Hope this helps.
You could try
if(time() <= $compy){
since your already doing strtotime() on whatever compy is originally. This way your working with 2 unix timestamps and comparing them that way.

Categories