This question already has answers here:
Get week number (in the year) from a date PHP
(18 answers)
Closed 9 years ago.
This is my code , everything works fine except $w which should be present week number .
$w="WEEKOFYEAR()";
I have declared everything and my code works when i hard code the week number
$w=13;
insert($actual,$target['value'],$country,$ini,$goal ,$comment,$an,$w,$con);
I get the following error : Instead of it showing week number 13; it is starting from 0
If you want to get the current week, you may try this
$weekNumber = date("W");
Doc: http://php.net/manual/en/function.date.php
The problem is that WEEKOFYEAR() is not a default PHP function. Use date() to get the present week number.
$w = date('W');
Note that the "W" is a capital W, it makes a difference.
Also, check out the PHP documentation for date(): http://php.net/manual/en/function.date.php
Related
This question already has answers here:
Subtracting two dates in php
(7 answers)
Closed 8 months ago.
I'm trying to make a program that subtracts the user's inputted birthdate and the date today.
Here is my code:
$bday = $_POST['bday']; //user input
$today=date("Y-m-d");
$myage= $today - $bday; //i got a warning message here saying "A non-numeric value encountered"
Any idea how to fix this?
Please try this snippet, Assuming you are requiring days and/or years as a result. I used the date_diff function for this code. Converting the user input into date/time( has the Y-m-d format) also I hard coded the user input for easier visualisation.
Working Snippet:
$input="2013-12-12"; #User input
$date = strtotime($input); #Convert user input to date time
$date1=date_create(date("Y-m-d")); #date today
$date2=date_create(date('Y-m-d', $date)); #inputted date
$diff=date_diff($date1,$date2); #date_diff function
echo $diff->format("%Y years or %a days");
Output:
8 years or 3123 days
There is possibly a cleaner version of this. But I believe this is the right flow for your requirements.
This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 3 years ago.
I have 2 dates. One is $monthToCheck = date('Y-m-01'); and the other one comes from an API and its stored in a variable : $reqs['end_date']. My question is : How do i check if the date from the api is bigger than $monthToCheck. With bigger i mean for example 2020 - 01 - 01 is bigger than 2019 - 12 - 01 because the year is higher.
What i tried :
$time = strtotime($reqs['end_date']);
$monthToCheck = date('Y-m-01');
if($time > $monthToCheck) {...}
I converted the date from the API to a date using strtotime and then compared it with my $monthToCheck variable.But i have a feeling something isnt right since the results are all "true" eventho that is not always the case.
Just put your $monthToCheck into strtotime():
$monthToCheck = strtotime(date('Y-m-01'));
Example
php can compare dates formatted year-mm-dd easily
if($monthToCheck > $reqs['end_date']){
// do something
}
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
I have two dates. One of them is the current date, one of them is a date somebody uploaded something, this is stored in a database. I need to find out if the date stored in the database is older than 7 days from the current date. I'm using PHP's date(d/m/y);, I've tried some things online, I've tried dateDifference() from php.net, I've tried converting them to timestamps and taking them away, but neither of these seem to work. Is there a simpler way?
This is what carbon is made for. Consider it.
//time in db is in this format 2018-03-16 08:31:09 for this example
$dateInDb = Carbon::createFromFormat("Y-m-d H:i:s",$timeInDb);
$days = Carbon::now()->diffInDays($dateInDb);
Check the library here
try this:
<?php
$upload_date = '09/03/2018'; # d/m/Y format
if (strtotime(date_format(date_create_from_format('d/m/Y',$upload_date),'Y-m-d')) < strtotime('-7 days')) {
echo 'Upload date is older than 7 days ago.';
} else {
echo 'Upload date is not older than 7 days ago.';
}
This question already has answers here:
convert month from name to number
(17 answers)
Closed 6 years ago.
I want to enter the full name of the month and in response i want to get the number of the month.
i have tried following code, when i pass the "March" as month name it return me "1".is there any way to get the number of the month in php?
$month_number = date('n', strtotime($string));
Try this :
<?php
$date = date_parse('March');
echo($date['month']);
?>
May this will help you :)
Try:
date('m', strtotime("March"));
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate person's current age
I want to find the persons age.
$time=$row['date_of_birth'];
$date=explode("-", $time);//gets the date birth from the database and puts it into an array
$a=getdate();//gets todays date
$diff_date= gregoriantojd($a["mon"],a["mday"],a["year"] ) - gregoriantojd(date[1],date[2],date[0]);//subtracts the differences between the dates, this is returned in seconds..
The question is whether this is the way to do it? and how do I convert the seconds and convert them to years (as an integer)?!
EDIT:
I think that the last line should be this:
return floor($diff_date /60*60*24*365);
Just try with strtotime (converts date string into timestamp):
$time = strtotime($row['date_of_birth']);
$age = date('Y', $time - time());
echo 'age: ' . $age;
If $time is supposed to be unix timestamp
$time=$row['date_of_birth'];
$date1 = new DateTime($time);
$date_diff = $date1->diff(new DateTime(), true);
$age = $date_diff->y;
I think you can make use of the date_diff function in PHP.
1st question: The question is whether this is the way to do it?
No. The function gregoriantojd (per the manual entry here) returns a value in days, not seconds. I like the date_diff conversion that Piotr gives the best.
2nd question: how do I convert the seconds and convert them to years
(as an integer)?!
Well, if you were starting with a number of seconds (which you aren't) then your second formula would be mostly correct, but of course it wouldn't account for leap-years. So for anyone older than 4, or at least 8, it would be off for one or more days around their birthday. Usually on their birthday is a pretty important time to get their age correct.