How to get a date in the current format? - php

How to get a date in the format 5 - 7 Jan
if (get_field('event_from')) {
echo $datefrom->format('d.m.Y').' - ';
}
return 21.01.2020 - 22.01.2020
But I need a date in a different format for example 5 - 7 Jan
Please help.

If the ACF-field is a datepicker, you could configure the return format in the field it self.
Setting it to j M will give you the value "5 Jan"
'j' is numeric representation of day without a leading zero.
'M' is a short textual representation of a month, three letters.
Check the PHP-manual for more information:
https://www.php.net/manual/en/function.date.php
Once the field is configured to return the date formated as above, you can just append the value to your string.
$date_string;
if (get_field('event_from')) {
$date_string = get_field('event_from') . ' - ';
}
if (get_field('event_to')) {
$date_string .= get_field('event_to');
}
echo $date_string;

Related

PHP: How to know if a date is in the current month?

I need to know if a date is in the current month.
Examples:
If the date is 2018-06-30 and current month is June (06), then true.
If the date is 2018-07-30 and current month is June (06), then false.
I have a list of dates with more than 1000 dates and I want to show or colorize only the dates that belongs to a current month.
You can do it all on one line. Basically convert the date in question to a PHP time, and get the month.
date('m',strtotime('2018-06-30' )) == date('m');
Using the date() function, if you pass in only the format, it'll assume the current date/time. You can pass in a second optional variable of a time() object to use in lieu of the current date/time.
I hope this helps -
$date = "2018-07-31";
if(date("m", strtotime($date)) == date("m"))
{
//if they are the same it will come here
}
else
{
// they aren't the same
}
As an alternative you could use a DateTime and for the format use for example the n to get the numeric representation of a month without leading zeros and use Y to get the full numeric representation of a year in 4 digits.
$d = DateTime::createFromFormat('Y-m-d', '2018-06-30');
$today = new DateTime();
if($d->format('n') === $today->format('n') && $d->format('Y') === $today->format('Y')) {
echo "Months match and year match";
}
Test
PHP doesn't implement a date type. If you are starting with a date/time and you know that your you are only dealing with a single timezone, AND you mean you want the current month in the curent year
$testdate=strtotime('2018-06-31 12:00'); // this will be converted to 2018-07-01
if (date('Ym')==date('Ym', $testdate)) {
// current month
} else {
// not current month
}

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

How to validate a date in PHP when the format can vary

I am working on a site and have to validate a date from a form. i am using jquery picker to select the date, however, the user can change the selection manually therefore i want to validate for any possible format (January, 1 2000, 1 jan 2000 etc).
I have looked around and am having trouble finding a solution. I am trying using strtottime to turn the date to a stamp and I am then using checkdate. The validation with strtotime works fine if i enter for instance January 40, 2000, however, if i enter a date that is below or equal to 31 with any month strtotime moves it to the following month, for instance, february 31, 2017 turns to March 3, 2017.
This is making the checkdate function useless as the date passed to it is always valid. is there a solution to this?
$thisD = 'February 31, 2017';
if (strtotime($thisD) === false)
{
echo "<BR>DATE FORMAT NOT VALID <BR>";
}
else
{
echo "<BR>DATE FORMAT VALID <BR>";
$thisDStr = strtotime($thisD);
echo "Original date was $thisD, date now is " . date('F, j Y',$thisDStr). ' -> ';
if (checkdate(date('n',$thisDStr), date('j',$thisDStr), date('Y',$thisDStr)))
{
echo ' is valid <br>';
}
else
{
echo ' is NOT valid <br>';
}
}//end else

Date format form Jan 30 to 2011-01-30

I have the following date format in a xml sheet:
Jan 30
and I want to display as:
2011-01-30
2011 being the current year
Can someone help me with that?
strtotime will use the current year if none is specified, so this would work
$t=strtotime("Jan 30");
echo strftime("%Y-%m-%d", $t);
strtotime + date gives you:
echo date("Y-m-d", strtotime('30 Jan')); //echoes '2011-01-30'
care to post the xml?
in anycase, take the string
$x = "Jan 30"
add 2011
$x = $x . ' ' . date('Y');
convert it to date time and format it
$y = date_format('Y-m-d', strtotime($x));
echo $y;`
strtotime is your key here, it converts most any date format into seconds since the epoch, then all you need do is use the date_format function
ps... if you like my answer, accept it as the answer to bring your accepted question ratio, otherwise people will be less likely to answer your questions

PHP: date function to get month of the current date

I want to be able to figure out the month of the current date variable. I'm ex vb.net and the way to do it there is just date.Month. How do I do this in PHP?
Thanks,
Jonesy
I used date_format($date, "m"); //01, 02..12
This is what I wanted, question now is how do I compare this to an int since $monthnumber = 01 just becomes 1
See http://php.net/date
date('m') or date('n') or date('F') ...
Update
m Numeric representation of a month, with leading zeros 01 through 12
n Numeric representation of a month, without leading zeros 1 through 12
F Alphabetic representation of a month January through December
....see the docs link for even more options.
What does your "data variable" look like? If it's like this:
$mydate = "2010-05-12 13:57:01";
You can simply do:
$month = date("m",strtotime($mydate));
For more information, take a look at date and strtotime.
EDIT:
To compare with an int, just do a date_format($date,"n"); which will give you the month without leading zero.
Alternatively, try one of these:
if((int)$month == 1)...
if(abs($month) == 1)...
Or something weird using ltrim, round, floor... but date_format() with "n" would be the best.
$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime);
echo date('y', $unixtime );
as date_format uses the same format as date ( http://www.php.net/manual/en/function.date.php ) the "Numeric representation of a month, without leading zeros" is a lowercase n .. so
echo date('n'); // "9"
As it's not specified if you mean the system's current date or the date held in a variable, I'll answer for latter with an example.
<?php
$dateAsString = "Wed, 11 Apr 2018 19:00:00 -0500";
// This converts it to a unix timestamp so that the date() function can work with it.
$dateAsUnixTimestamp = strtotime($dateAsString);
// Output it month is various formats according to http://php.net/date
echo date('M',$dateAsUnixTimestamp);
// Will output Apr
echo date('n',$dateAsUnixTimestamp);
// Will output 4
echo date('m',$dateAsUnixTimestamp);
// Will output 04
?>
To compare with an int do this:
<?php
$date = date("m");
$dateToCompareTo = 05;
if (strval($date) == strval($dateToCompareTo)) {
echo "They are the same";
}
?>

Categories