Basically that is the function that I use to extract the date from the php and modify it:
function getAge()
{
$result= mysql_query("SELECT date_of_birth
FROM controlpanel
WHERE user_id=".$this->userID) or die(mysql_error());
$year="";
if($row=mysql_fetch_array($result))
{
$row['date_of_birth'];
}
}
$row['date_of_birth']; I want to get the year from that row, and check if it is 0000..if it is I want to return return "Not given";.. if it isnt, I want to compare the full date d-m-y to todays date.. in order to get the persons birth date..however, I am not sure what date functions to use..
if( substr($row['date_of_birth'],0,4) == "0000") echo "Not given";
$year = date("Y", $row['date_of_birth']);
if ($year == 0000) {
return "Not given";
}
return date("d m Y", $row['date_of_birth']);
this will give you the 4 digit year from your result:
$year = date('Y', strtotime($row['date_of_birth']);
then, you can use a function like the one on the following page to get the age:
http://www.geekpedia.com/code79_Calculate-age-from-birth-date.html
or other solutions:
http://www.google.com/search?gcx=w&sourceid=chrome&ie=UTF-8&q=php+get+age+from+birthday
need to add some quotes to declare the 0000 as string cause if you use 0000 not "0000" php will just read it as 0
$year = date("Y", $row['date_of_birth']);
if ($year == "0000") {
return "Invalid year";
}
else{
return "Valid year";
}
Related
how to understand if one date in php is less than another minus one day? I mean if for example a date is set to "2018/07/03"; how can I understand if a given date is less than "2018/07/02"
date1 : year1/month1/day1
date2: year2/month2/day2
<?php
if ($year1 >= $year2) {
if ($month1 >= $month2) {
if (($day1 - 1) > $day2) {
echo 'you could do something..';
}
}
}
?>
the above code fails if forexample $year2 = 2017 and $month2 = 11.. can anybody help me? thanks a lot..
Here, this should work.
$date_to_check = new DateTime($yesterday);
$today = new DateTime();
$time_diff = $today->diff($date_to_check)->d;
if($time_diff > 1) {
echo "This is greater than one day.";
}else{
echo "This is not greater than one day.";
$date = strtotime("2018/07/01");
$date2 = strtotime("2018/07/02");
if($date > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger');
}
To convert string to date php has function named strtotime().
Compairing date objects is simple.
There is full information about strtotime()
http://php.net/manual/ru/function.strtotime.php
Another way:
$date = new DateTime("2018/07/01");
$date2 = new DateTime("2018/07/02");
if($date->modify("+1day") > $date2){
print('date is bigger');
// do stuff when date is bigger than date2
} else {
// else ...
print('date2 is bigger or equal');
}
Notice modify modifies $date object itself.
Read more here http://php.net/manual/en/class.datetime.php
I've got a problem with datetime condition checked.
First, I've a DateTime value that stored in database as 2018-05-08 15:54:40
But, I want to check only date is equal or not.
For example:
$DateInDatabase = 2018-05-08 15:54:40
$DateSpecific = 2018-05-08
if ($DateInDatabase == $DateSpecific) {
......
}
The question is how to check only date in $DateInDatabase
If you are only interested in the year-month-day, my suggestion would be something such as;
// As per comments, the second param in the date function MUST be a UNIX timestamp, so strtotime will resolve this
$dateInDB = date("Y-m-d", strtotime($DateInDatabase)); // format to XXXX-XX-XX
$dateToCheck = date("Y-m-d", strtotime($DateSpecific)); // format to XXXX-XX-XX
if ($dateInDb == $dateToCheck)
{
// They are the same
}
else
{
// The are different
}
As others have said, you can use direct string comparison also;
$DateInDatabase = "2018-05-08 15:54:40";
$DateSpecific = "2018-05-08";
// This function uses the params haystack, needle in that order
if (stristr($DateInDatabase, $DateSpecific))
{
// Match found
}
else
{
// No match found
}
You can use php date method to format date:
$DateInDatabase ='2018-05-08 15:54:40';
$DateSpecific = '2018-05-08';
if (date('Y-m-d', strtotime($DateInDatabase)) == $DateSpecific) {
echo 'ok';
} else {
echo 'not ok';
}
Read more about php date method from here
You can either directly match as string or parse as date and later format as string.
Given that $DateInDatabase = '2018-05-08 15:54:40' and $DateSpecific = '2018-05-08'
if (false !== strpos($DateInDatabase,$DateSpecific)) {
/* matched */
}
OR
$format = 'Ymd';
$DateInDatabase = (new DateTime($DateInDatabase))->format($format);
$DateSpecific = (new DateTime($DateSpecific))->format($format);
if ($DateInDatabase === $DateSpecific) {
/* matched */
}
This will check if your dates are equal. Be aware that PHP's date function has a limit in how far you can go for the year. The minimum is 01-01-1970 and the maximum is 19-01-2038 (d-m-Y format used here). The date is converted to an integer (strtotime). If those numbers are equal, the dates are equal.
$DateInDatabase = '2018-05-08 15:54:40'
$DateSpecific = '2018-05-08'
$newDate1 = date("Y-m-d", strtotime($DateInDatabase));
$newDate2 = date("Y-m-d", strtotime($DateSpecific));
if ($newDate1 == $newDate2) {
//equal
} else {
//not equal
}
I have following format 2016-06-06 TO 2016-06-12
I want to know that whether my date i.e 2016-06-11 lies in between or not. How can I do this?
You can use PHP if condition for checking date range:
$compareDate = "2016-06-06 TO 2016-06-12";
$dateArr = explode(" TO ",$compareDate);
$starting_date = $dateArr[0];
$final_date = $dateArr[1];
$date = "2016-06-11";
if($date >= $starting_date && $date <= $final_date) {
...
}
if($date >= $fome_date && $date <= $to_date)
{
echo "yes";
}
else
{
echo "no";
}
https://3v4l.org/UdIgq
i hope it will be helpful
Dates can be compared just as numbers can be
So using
date > starting_date && date < final_date
Will be just fine for an if clause. Also if you have I would recommend you do this in the database part as dbs have built in queries for such occasions.
I have a date in the form MM-YYYY (e.g: 04-2000). I exploded the date into month and year and am now trying to check certain conditions on the month:
between 1 and 12
formed of 2 digits)
and on the year:
formed of 4 digits
Is my syntax correct?
list($name_month, $name_year) = explode('-', $name_date, 2);
if(($name_month < 1 || $name_month > 12 || $name_month ) || ($name_year)) {
echo "<br><br>Wrong date";
$uploadOk = 0;}
You can use the DateTime object and createFromFormat() to validate your date:
$date = '04-2000';
// Create a DateTime object pointing to the 1st of your given month and year
$d = DateTime::createFromFormat('d-m-Y', '01-' . $date);
if($d && $d->format('m-Y') == $date){
echo 'Valid date';
}
Eval.in
This should work, if seprator is always -
$date = '04-2000';
if($date == date('m-Y', strtotime('01-'.$date)))
{
echo 'Valid date';
}
As commented, there are several was to do this. What first comes to mind is checkdate() and createFromFormat().
The catch is, you need to be mindful what is injected for the day part since your format does not include day.
Since createFromFormat() injects the day part from the current date, it is not a viable option. George's code will fail for a format of 02-2015 on days after 28.
As such, I would use checkdate():
$date = '04-2000';
list($month, $year) = explode('-', $date);
if (checkdate($month, 1, $year)) {
echo 'Valid date';
}
I only want to show some html on certain days of the week. I think I am very close, but cannot get this to work. Thanks for the help.
<?php
$dayofweek = date('l');
$daystoshow = array('Thursday','Sunday','Wednesday');
if ($dayofweek == $daystoshow) {
echo "show this";
}
?>
Use in_array() to check to see if a value in in an array:
if (in_array($dayofweek, $daystoshow)) {
Allright:
Lets use PHP date function
Do you want to show it only during a particular month ?
$date = date("m");
if ($date == "01") { // only on january
//output
}
Do you want to show it only during a particular day ?
$date = date("d");
if ($date == "01") { //Only on the first of the month
//output
}
Do you want to show it only during particular dayS ?
$days = array("01","11","28");
$date = date("d");
if (in_array($date,$days)) { //Only 01,11,28
//output
}
Do you want to show it only during particular days of week ?
$days = array("Thursday","Sunday","Wednesday");;
$date = date("l");
if (in_array($date,$days)) {
//output
}
Do you want to show it only during a particular year ?
$date = date("Y");
if ($date == "2014") { //Only on 2014
//output
}