I have searched through SO but the answers that I've tried doesn't seem to solve my problem.
I have this simple code snippet where the user will input a numeric date, and a month, and the app will return the corresponding Zodiac Sign.
$birthdate = $_POST["birthdate"];
$birthmonth = (ucwords(strtolower($_POST["month"])))
//validations here. . .
$tmp = $birthmonth . " " . $birthdate;
$tmp2 = date_create_from_format('M j', $tmp);
$formatted_dob = date_format($tmp2, 'm-d-Y');
$dob = strtotime($formatted_dob);
echo $formatted_dob;
if ($dob >= strtotime('01-20-2016') && $dob <= strtotime('02-18-2016')) {
echo "Aquarius";
} elseif ($dob >= strtotime('02-19-2016') && $dob <= strtotime('03-20-2016')){
echo "Pisces";
}
Those echo stuff outside the if-else block are working fine, however if I input a value of 25 and February (which later on results to 02-25-2016), it always output Aquarius. How do you compare two strtotimevalues?
I've tried using DateTime object but it only gives me an error, which is another story. Thanks in advance.
Edited:
Change the order of your date (*your format on your date 01-20-2016 m-d-Y that's why when you convert it it becomes 1970-01-01 'Y-m-d' but if you change it into 2016-01-20 'Y-m-d' on your date range the code will work just fine in else-if.
$birthdate = $_POST["birthdate"];
$birthmonth = (ucwords(strtolower($_POST["month"])))
//validations here. . .
$tmp = $birthmonth . " " . $birthdate;
$tmp2 = date_create_from_format('M j', $tmp);
$formatted_dob = date_format($tmp2, 'm-d-Y');
$dob = strtotime($formatted_dob);
echo $formatted_dob;
$dobcompare = date_create(date('m/d/Y', $dob));
$aqstartdate = date_create(date('m/d/Y', strtotime('2016-01-20')));
$aqenddate = date_create(date('m/d/Y', strtotime('2016-02-18')));
$pistartdate = date_create(date('m/d/Y', strtotime('2016-02-19')));
$pienddate = date_create(date('m/d/Y', strtotime('2016-03-20')));
if ($dobcompare >= $aqstartdate && $dobcompare <= $aqenddate) {
echo "Aquarius";
}
elseif ($dobcompare >= $pistartdate && $dobcompare <= $pienddate) {
echo "Pisces";
} else {
echo "IDK";
}
Modify it in your need.
This is the example enter link description here
Related
I want to check if a date/time string contains a day or not.
I used the date_parse() function but it automatically adds day => 1 if it doesn't find any day.
So I couldn't know if a string contains a day or not.
For example
$date = "2021-02";
How to know if this string contains a day or not?
The DateTime::createFromFormat method tests well that the format is adhered to. Entries like "2019-02-" or "2019-xx-23" are also recognized as incorrect.
$date = "2021-02-x";
$dateTime = DateTime::createFromFormat('Y-m-d',$date);
if($dateTime){
echo $date.' ok';
}
else {
echo 'wrong date '.$date;
}
I think you are expecting some thing like this.
<?php
$date1 = "2021-02";
$date2 = "2021-02-11";
$date3 = "2021-12";
$date4 = "2021-12-14";
$date1_array = explode("-", $date1);
$date2_array = explode("-", $date2);
$date3_array = explode("-", $date3);
$date4_array = explode("-", $date4);
if (count ($date1_array) == 3)
{
echo $date1 . ": It's a Date.";
echo "<br />";
}
if (count ($date2_array) == 3)
{
echo $date2 . ": It's a Date.";
echo "<br />";
}
if (count ($date3_array) == 3)
{
echo $date3 . ": It's a Date.";
echo "<br />";
}
if (count ($date4_array) == 3)
{
echo $date4 . ": It's a Date.";
echo "<br />";
}
?>
i'm trying to do a contract management app using php and mysql and i'm having some questions regarding the dates.
I need to know the time that there is between today and specific dates in the contract, or if there is less than a month it should display days left..
the problem is that the comparison to know if the end of contract is in the past or in the future, does'n seems to work!
link to check the code: link to project
$hoje = date_create();
$fim = '2022-11-11';
$fim_data = date_create($fim);
$diff = date_diff( $hoje, $fim_data );
$meses = (($diff->format('%y')*12)+$diff->format('%m'));
$dias = $diff->days;
var_dump($fim < $hoje);
if($fim < $hoje) {
$result = "Contract has ended";
} elseif($meses >=1 ) {
$result = $meses . " months";
echo '<br>';
} else {
$result = $dias . " days";
};
echo '<br>';
echo $result;
You are comparing string with date object
Replace
if($fim < $hoje) {
With
if($fim_data < $hoje) {
Corrected code with solution by Felippe Duarte
$hoje = date_create();
$fim = '2018-11-11';
$fim_data = date_create($fim);
$diff = date_diff( $hoje, $fim_data );
$meses = (($diff->format('%y')*12)+$diff->format('%m'));
$dias = $diff->days;
var_dump($fim_data < $hoje);
if($fim_data < $hoje){$result = "não aplicavel";}
elseif($meses >=1 ){
$result = $meses . " meses";
echo '<br>';}
else{
$result = $dias . " dias";};
echo '<br>';
echo $result;
I want to set up 2 cookies. One that shows how many times a user entered the page, and the other shows the date and the time of last visit.
The code works fine, but I need my cookies set up once a day, not every time I visit the page. What I need is to set the if($_COOKIE['lastTime'] != date('d-M-Y')) statement, but I need to compare only the 'd-M-Y' part of $_COOKIE['lastTime']. Also, I can't set $lastTime = date('d-M-Y') (without h-i-m-s), because I need to show the date AND the time of last visit.
Please, help.
$counter = 0;
if(isset($_COOKIE['counter'])){
$counter = $_COOKIE['counter'];
}
$counter++;
$lastTime = '';
if(isset($_COOKIE['lastTime'])){
$lastTime = $_COOKIE['lastTime'];
}
$lastTime = date('d-M-Y H-i-m-s');
if($_COOKIE['lastTime'] != date('d-M-Y')){
setcookie('counter', $counter);
setcookie('lastTime', $lastTime);
}
print_r('This is your ' . $_COOKIE['counter'] . ' visit');
echo '<br>';
print_r('Last visit was: ' . $_COOKIE['lastTime']);
I left commented lines in the scope, so you can see what can be optimised to get brevity to your code. Those commented lines are not needed at all.
print_r('Previous visit was: ' . isset($_COOKIE['lastTime'] ? date("Y-m-d", $_COOKIE['lastTime']) : 'not set yet'));
$counter = 0;
if(isset($_COOKIE['counter'])){
$counter = $_COOKIE['counter'];
}
$counter++;
$lastDay = '';
if(isset($_COOKIE['lastTime']) {
$lastDay = date("Y-m-d", $_COOKIE['lastTime']);
}
$currentDay = date("Y-m-d");
if(!$lastDay || $lastDay < $currentDay) {
setcookie('counter', $counter);
setcookie('lastTime', strtotime($currentDay));
}
print_r('This is your ' . $_COOKIE['counter'] . ' visit');
echo '<br>';
// here you print a readable datetime format from your stored date
print_r('Last stored visit date was: ' . date("Y-m-d", $_COOKIE['lastTime']));
use this:
$lastTime = date('d-M-Y');
and not this:
$lastTime = date('d-M-Y H-i-m-s');
Your code:
$lastTime = '';
if(isset($_COOKIE['lastTime'])){
$lastTime = $_COOKIE['lastTime'];
}
$lastTime = date('d-M-Y H-i-m-s'); <-- you're overwriting any retrieved value here
Check if you got a value first so you don't overwrite it, and then extract just the date part:
$lastTime = '';
if(isset($_COOKIE['lastTime'])){
$lastTime = $_COOKIE['lastTime'];
}
if(strlen($lastTime) == 0)
$lastTime = date('d-M-Y H-i-m-s');
$parts = explode(" ", $lastTime);
$lastDay = $parts[0];
if($lastDay != date('d-M-Y')){...
I'm getting dates from a Wordpress field and I need to check if the dates have past or still to come.
$dates = ['date'=>'02/12/13','date'=>'10/12/14','date'=>'14/01/15'];
foreach ($dates as $date){
$the_date = $date['date'];
echo $the_date;
echo " ";
echo date('d/m/y');
echo " ";
if($the_date < date('d/m/y')){
echo 'gone';
}else{
echo 'to come';
}
}
The foreach echos out this.
02/12/13 22/11/14 gone
10/12/14 22/11/14 gone
14/01/15 22/11/14 gone
27/01/15 22/11/14 to come
10/02/15 22/11/14 gone
It looks like it's just checking the first day date.
A better option is to use the DateTime class. It allows to compare two DateTime instances using comparison operators.
$dates = ['02/12/13','10/12/14','14/01/15'];
foreach ($dates as $date) {
$the_date = \DateTime::createFromFormat('d/m/y', $date);
$now = new \DateTime();
echo $date." ".($the_date < $now ? 'gone' : 'to come')."\n";
}
The problem you see is because the dates are being compared as strings. The current date is "22/11/14" so it will be greater than any other date with a day starting with "1" or "0".
PD: Your array contains many elements using the same 'date' key. That is a problem so I've removed them in my example.
<?php
$dates = array('02/12/13','10/12/14','14/01/15');
$now = mktime(0,0,0);
foreach($dates as $date) {
$tmp = explode('/',$date);
$date_time = mktime(0,0,0,intval($tmp[1]),intval($tmp[0]),intval($tmp[2]));
echo $date . ' ' . ($now > $date_time?'gone':'to come') . "\n";
}
Use PHP's DateTime API :
$date='02/12/13';
if(\DateTime::createFromFormat('d/m/y',$date) < new \DateTime()){
//date is in the past
}else{
//date is either today or in the future
}
Offical PHP doc:
http://php.net/manual/en/class.datetime.php
Best way is to use timestamp: Try this:
foreach ($dates as $date){
$the_date = $date['date'];
echo $the_date;
echo " ";
echo date('d/m/y');
echo " ";
if( strtotime($the_date) < time() )
{
echo ' is gone';
}
else
{
echo ' is to come';
}
}
Keep her simple:
// $date is the date you need to compare to today
$date = ("2015 10 03");
// Make sure their formats are purely numeric and match
if ($date->format('m.d.y') >= date('m.d.y'))
{
your procedure...
}
I suggest using the capabilities of the DateTime class instead. Then you can do the check as follows:
<?php
$then = $reset_date;
$then = new DateTime($then);
$now = new DateTime(date("m-d-Y"));
$sinceThen = $then->diff($now);
$new = new DateTime($reset_date);
$old = new DateTime(date("m-d-Y"));
if ( $old->modify('+1 year') < $new) {
echo "<font color='red'>Reset now <br></font>";
echo "<font color='orange'>$sinceThen->y years <br></font>";
echo "<font color='orange'>$sinceThen->m months </font>";
echo "<font color='orange'>$sinceThen->d days have passed.<br></font>";
} else {
echo "<font color='green'> $sinceThen->y years <br>
$sinceThen->m months $sinceThen->d days till to Reset.</font>";
//Combined
}
?>
if(isset($_POST['submit_event'])){
$m = $_POST['event_month'];
$y = $_POST['event_year'];
$d = $_POST['event_day'];
$date = date('Y-n-d',strtotime($y. '-' .$m. '-' .$d));
echo $date;
//i always get 2013-10-07
}
All my inputted datas are correct although the output is always wrong and the same.
if (isset($_POST['submit_event']) && isset($_POST['event_month']) && isset($_POST['event_year']) && isset($_POST['event_day'])) {
$m = $_POST['event_month'];
$y = $_POST['event_year'];
$d = $_POST['event_day'];
$date_pre = $y. '-' .$m. '-' .$d;
$time = strtotime($date_pre)
$date = date('Y-n-d', $time);
echo $date;
}
// For debugging:
else {
echo "Not all variables have been set."
}