Wordpress IXR_Date wrong date - php

This is my php code:
echo date('m/d/Y h:i:s A T'),"<br>";
$match_date = strtotime(date('m/d/Y h:i:s A T'));
echo "match_date= ", $match_date, "<br>";
$test = new IXR_Date($match_date);
print_r($test);
But return wrong month and day value:
08/19/2012 07:38:10 AM PDT
match_date= 1345387090
IXR_Date Object ( [year] => 2012 [month] => 2012 [day] => 2012 [hour] => 07 [minute] => 38 [second] => 10 )
Whats wrong? How can I use IXR_Date properly?

Use time() function instread of strtotime over date:
$test = new IXR_Date(time());

Related

Add 3 month to the given date in a loop PHP

Hi i have the following date = 2015-06-01 00:00:00
so i have to add 3 month to this date , so write the following code
$sarting="2015-06-01 00:00:00";
$date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
And i get the output = 2015-09-01;
But now i want to add 3 month again and again for 20 times and i have to store the output to array
so i write the following code
$store_date=array();
for($i=0;$i<20;$i++){
$store_date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
}
But the thing it's returning 2015-09-01 20 times . i need like this 2015-09-01, 2015-12-01,2016-06-01 etc .
Please check
I would recommend using the DateTime-class so we can reuse the same instance on each iteration.
$sarting = "2015-06-01 00:00:00";
$store_date = [];
// Create a DateTime-object we can reuse
$date = new DateTime($sarting);
for ($i = 0; $i < 20; $i++) {
$date->modify('+3 months');
$store_date[] = $date->format('Y-m-d');
}
print_r($store_date);
Since we're resuing the same object, it will keep adding 3 months to it on each iteration. No need for any calculations or anything, making the code clean and readable.
Demo: https://3v4l.org/KZiH9
You can use PHP's dateTime class' modify() method to add +3 months in loop to it.
<?php
$dateStamp = "2015-06-01 00:00:00";
$date = new DateTime($dateStamp);
$datesArr = [];
for ($i=1; $i<21 ; $i++) {
$date->modify('+3 month');
$datesArr[] = $date->format('Y-m-d h:i:s');
}
echo '<pre>';
print_r($datesArr);
echo '</pre>';
Output:
Array
(
[0] => 2015-09-01 12:00:00
[1] => 2015-12-01 12:00:00
[2] => 2016-03-01 12:00:00
[3] => 2016-06-01 12:00:00
[4] => 2016-09-01 12:00:00
[5] => 2016-12-01 12:00:00
[6] => 2017-03-01 12:00:00
[7] => 2017-06-01 12:00:00
[8] => 2017-09-01 12:00:00
[9] => 2017-12-01 12:00:00
[10] => 2018-03-01 12:00:00
[11] => 2018-06-01 12:00:00
[12] => 2018-09-01 12:00:00
[13] => 2018-12-01 12:00:00
[14] => 2019-03-01 12:00:00
[15] => 2019-06-01 12:00:00
[16] => 2019-09-01 12:00:00
[17] => 2019-12-01 12:00:00
[18] => 2020-03-01 12:00:00
[19] => 2020-06-01 12:00:00
)
Working Code:
You are not changing the value of $sarting. Try this:
$sarting = "2015-06-01 00:00:00";
$store_date = array();
for ($i = 0; $i < 20; $i++) {
$sarting = $store_date[$i] = date('Y-m-d', strtotime($sarting . "+3 months"));
}
Try this check if it helps,
$sarting="2015-06-01 00:00:00";
$store_date=array();
for($i=0;$i<20;$i++){
$store_date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
$sarting=$store_date[$i];
}
echo "<pre>";
print_r($store_date);
echo "</pre>";
Below code for Addition or Subtraction for Year/Month/day from date
Addition = date('Y-m-d', strtotime("+0 years +3 months +0 days", strtotime($lastdate)));
Subtraction = date('Y-m-d', strtotime("+0 years -3 months +0 days", strtotime($lastdate)));
$lastdate = "01-12-2015"; //actual date
$after_3_month = date('Y-m-d', strtotime("+0 years +3 months +0 days", strtotime($lastdate))); // adding 3 month on the actual date
echo $after_3_month; //3 month added with actual date

Issue with DatePeriod Class Output

I'm using DatePeriod to get date difference between two dates but it creates issue when I need to get data Year based format. Following is my work
$start_date = '2016-08-29 21:47:33';
$end_date = '2018-04-28 17:30:55';
function getDateRange($start_date, $end_date, $type, $format = 'Y-m-d')
{
$period = new DatePeriod(
new DateTime($start_date),
new DateInterval("P1{$type}"),
new DateTime($end_date)
);
$dates = [];
foreach($period as $dt)
{
$dates[] = $dt->format("$format");
}
return $dates;
}
print_r(getDateRange($start_date, $end_date, 'Y', 'Y'));
According to my function it should return
Array
(
[0] => 2016
[1] => 2017
[2] => 2018
)
But its been outputting only two of them
Array
(
[0] => 2016
[1] => 2017
)
If I'm using output format with Month & Year i.e. M Y with interval of Month then it works fine output for that
print_r(getDateRange($start_date, $end_date, 'M', 'M Y'));
Array
(
[0] => Aug 2016
[1] => Sep 2016
[2] => Oct 2016
[3] => Nov 2016
[4] => Dec 2016
[5] => Jan 2017
[6] => Mar 2017
[7] => Apr 2017
[8] => May 2017
[9] => Jun 2017
[10] => Jul 2017
[11] => Aug 2017
[12] => Sep 2017
[13] => Oct 2017
[14] => Nov 2017
[15] => Dec 2017
[16] => Jan 2018
[17] => Feb 2018
[18] => Mar 2018
[19] => Apr 2018
)
I have modified changes as per documentation link,
Here is the code,
function getDateRange($start_date, $end_date, $type, $format = 'Y-m-d')
{
$start_date = date_create_from_format("Y-m-d H:i:s", $start_date);
$end_date = date_create_from_format("Y-m-d H:i:s", $end_date);
$endDateInt = new DateInterval( "P1Y" );
$end_date->add( $endDateInt );
$period = new DatePeriod(
$start_date,
new DateInterval("P1{$type}"),
$end_date
);
$dates = [];
foreach($period as $dt)
{
$dates[] = $dt->format("$format");
}
return $dates;
}
$start_date = '2016-08-29 21:47:33';
$end_date = '2018-04-28 17:30:55';
print_r(getDateRange($start_date, $end_date, 'Y', 'Y'));
Here is working link.
You can see the reason why in your second output. The last date goes only until April, and the first one is in August. So it's indeed not two full years, and your first result is correct. If you want to make it be the number of "years involved", without regard for actual difference, you could simulate this by setting the second date to the same day and month as the first one, if $type is Y:
<?php
$start_date = '2016-08-29 23:47:33';
$end_date = '2018-09-28 17:30:55';
function getDateRange($start_date, $end_date, $type, $format = 'Y-m-d')
{
$start = new DateTime($start_date);
$end = new DateTime($end_date);
if ($type === "Y") {
$start->setTime(0, 0, 0);
$end->setDate($end->format("Y"), $start->format("m"), $start->format("d"));
$end->setTime(0, 0, 1);
}
$period = new DatePeriod(
$start,
new DateInterval("P1{$type}"),
$end
);
$dates = [];
foreach($period as $dt)
{
$dates[] = $dt->format("$format");
}
return $dates;
}
print_r(getDateRange($start_date, $end_date, 'Y', 'Y'));
Demo

Extract the date from string in PHP [duplicate]

This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Closed 8 years ago.
I have the creation time in following format:
Mon Mar 25 2013 15:28:21 GMT+0000 (GMT Standard Time)
How can I use PHP to extract the date from this string?
$dateTime = DateTime::createFromFormat('D M d Y H:i:s e', 'Mon Mar 25 2013 15:28:21 GMT+0000');
$dateTime->setTimezone(new DateTimeZone('UTC'));
echo $dateTime->format('Y-m-d H:i:s');
Note this requires at least PHP 5.3.0, for more information see documentation at http://www.php.net/manual/en/datetime.createfromformat.php
See http://php.net/manual/en/function.date-parse.php
Example #1 A date_parse() example
<?php
print_r(date_parse("2006-12-12 10:00:00.5"));
?>
The above example will output:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
From here on, you should be able to parse Mon Mar 25 2013 15:28:21 GMT+0000. Or consider using explode(' ', $parts) to split it into a $parts[2]='25' array.

Year value error from PHP date() [duplicate]

This question already has answers here:
Display all the week numbers between two dates in PHP [duplicate]
(3 answers)
Closed 8 years ago.
Given a start and end date, I need to generate an array with year and week values.
For example:
Start Date: 2013-01-01
End Date: 2013-02-23
Generated Array:
Array ( [0] => Array ( [week] => 01 [year] => 2013 )
[1] => Array ( [week] => 02 [year] => 2013 )
[2] => Array ( [week] => 03 [year] => 2013 )
[3] => Array ( [week] => 04 [year] => 2013 )
[4] => Array ( [week] => 05 [year] => 2013 )
[5] => Array ( [week] => 06 [year] => 2013 )
[6] => Array ( [week] => 07 [year] => 2013 )
[7] => Array ( [week] => 08 [year] => 2013 ) )
Here is the code I'm using to generate this:
public static function getYearWeekRange($startdate, $enddate) {
$array = array();
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
while ($starttime <= $endtime) {
$year = date('Y', $starttime);
$week = date('W', $starttime);
$array[] = array('week' => $week, 'year' => $year);
$starttime = strtotime('+1 week', $starttime);
}
return $array;
}
My problem is that when I generate certain date ranges, I don't get the correct year value at the start of the 2013 year. For example:
Start Date: 2012-01-01
End Date: 2013-02-23
In this case, where it should have an subarray with year = 2013 and week = 01, it actually has it's year value equal to 2012.
If I were to switch the start date to 2013-01-05 for example, then there is no problem.
Can anyone offer a solution that would guarantee that my year and week values are always correct?
I was able to fix my problem using the following:
public static function getWeekRange($startdate, $enddate) {
$array = array();
$p = new DatePeriod(
new DateTime($startdate),
new DateInterval('P1W'),
new DateTime($enddate)
);
foreach ($p as $w) {
$array[] = array('year' => $w->format('o'), 'week' => $w->format('W'));
}
return $array;
}

break apart formatted date into individual month/day/year hour/minute/second in php

I have a date in the following format
November 18, 2009, 3:00PM
How can i break that up so that i can store each value as its own variable?
such as...
$month //November
$day //18
$year //2009
$hour //03
$minute //00
$ampm //PM
Use the 'date_parse' (http://nl2.php.net/manual/en/function.date-parse.php) function. It returns an array with the parsed items:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
Convert your date into a timestamp, then with the timestamp you can easily get your parts. An other way is using a regular expression.
$str = "November 18, 2009, 3:00PM";
list($month,$day,$year,$time) = preg_split('/[ ,]/',$str,false,PREG_SPLIT_NO_EMPTY);
preg_match('/([0-9]+):([0-9]+)([AP]M)/',$time,$timeparts);
list($time,$hour,$minute,$ampm) = $timeparts;
echo "\$month $month\n";
echo "\$day $day\n";
echo "\$year $year\n";
echo "\$hour $hour\n";
echo "\$minute $minute\n";
echo "\$ampm $ampm\n";
Output
$month November
$day 18
$year 2009
$hour 3
$minute 00
$ampm PM
More complex solution.
If your dates may be in the different standards you can use date() function (http://php.net/manual/en/function.date.php) + strtotime() function (http://php.net/manual/en/function.strtotime.php), which parse string and returns the unix timestamp.
For example, if you want to get a year from your date string you could write next code:
$date = 'November 18, 2009, 3:00PM';
$year = date('Y', strtotime($date));
Or, if you want to know how much days in the month in date you get, you could write such code:
$date = 'November 18, 2009, 3:00PM';
$num_of_days = date('t', strtotime($date));
't' returns the number of days in the given month.

Categories