Code
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date']);
$order_dispatch_date = $order_dispatch_date->format('Ymd');
$order_collection = date('Ymd', strtotime('next thursday', $order_dispatch_date));
$order_collection = new DateTime($order_collection);
echo '['.$order_collection->format('d/m/Y') . ' - ' . $order_dispatch_date . ' - ' . gettype($order_dispatch_date) . ' ] ';
I get the following output if the input is: 20171128
[29/08/1970 - 20171128 - string ]
My question is why is the first output showing 29th August 1970 rather than 20th November 2017?
Stop changing things back-and-forth between objects, strings and timestamps so much, especially when you're re-using the same variables. It's needlessly hard to follow.
If you've got a Ymd formatted string, and you want an equivalent string back for the proceeding Thursday, just use this:
$inc['order_dispatch_date'] = '20171128';
$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date'])->modify('next thursday');
echo $order_dispatch_date->format('Ymd');
// 20171130
See https://eval.in/909998
Basically:
$order_collection = date('Ymd', strtotime('next thursday', strtotime($order_dispatch_date)));
And that should work :)
Related
I only have 2 strings, year and month. And I need to get the first and last day using Carbon. For example, '2020' and '3' are provided. I would need to get 2 Carbon dates out of these (2020-3-1 and 2020-3-31). Is it possible?
To achieve your goal use the following codes:
use Carbon\Carbon; // imports the Carbon class
$year = 2020;
$month = 3;
$date_1 = Carbon::create($year, $month)->startOfMonth()->format('Y-m-d'); //returns 2020-03-01
$date_2 = Carbon::create($year, $month)->lastOfMonth()->format('Y-m-d'); //returns 2020-03-31
Happy coding :)
you can do it simply with carbon methods like startOfMonth and endOfMonth as below
$startOfMonth=\Carbon\Carbon::parse('2020-3')->startOfMonth()->format('Y-n-d');
$endOfMonth=\Carbon\Carbon::parse('2020-3')->endOfMonth()->format('Y-n-d');
As per Carbon Documentation
Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
The only special case is for create() that has minimum value as
default for missing argument but default on current value when you
pass explicitly null.
So if you pass only $year and $month it will automatically considered first day and then you can format() function it to get the first day as day for e.g.
Carbon::create($year, $month)->format("Y-m-d")
I use this method to get the 1st and last day of the month.
t represents the last day of the month.
$posts = Post::whereBetween('created_at', [
Carbon::createFromDate(date('Y-m-d 00:00:00', strtotime(request('year') . '-' . request('month') . '-1'))),
Carbon::createFromDate(date('Y-m-d 23:59:59', strtotime(request('year') . '-' . request('month') . '-t')))])
->get();
return $posts
Edit:
I use + on the php but it is . sorry.
or you can use this:
$this->client = $this->client->whereBetween('created_at', [
Carbon::createFromDate(date(request('year') . '-' . request('month') . '-01 00:00:00')),
Carbon::createFromDate(date(request('year') . '-' . request('month') . '-12 23:59:59'))])
->get();
Edit:(Edit:)
Base on your question you need to get the first and last day.
But in the real world the 1st day is given so you need only to get the last day.
It should be like this.
The first day should be given because we all know that it falls to 1.
$firstDayOfTheMonth = date(request('year') . '-' . request('month') . '-1 00:00:00');
$lastDayOfTheMonth = date(request('year') . '-' . request('month') . '-t 23:59:59');
I would like my output to be - 05/12/2016 <--- year depends on the current year.
$date='05/12/2014';
$current_year='date("Y")';
I've tried combine like below but clearly its wrong. Please help me, how do I combine these two to get my desire output?
$date =(date("d/m", strtotime($date'])) / date("Y"));
$date='05/12/2014';
echo date("d/m", strtotime($date)) ."/". date("Y");
You should try something like this. Concate your / and remove ] .You need to format your database date.It should work.Try:
$databaseDate = date("d/m/Y", strtotime("31/12/2014")); //you need to format your databse Date
$mergeDate = date("d/m", strtotime($databaseTime)) . '/' . date("Y");
echo $mergeDate;
LIVE DEMO
Your approach can work. Just adapt it a bit:
date("d/m", strtotime($date)) . '/' . date("Y");
EDIT:
As pointed out by others, with this type of date format (d/m/Y) you might have issues when date is '25/12/2014'. To be sure your code works in any case use date_parse_from_format to control your date format:
$date = '25/12/2014';
$date_array = date_parse_from_format('d/m/Y',$date);
echo $date_array['day'] . '/' . $date_array['month'] . '/' . date("Y");
I'd be happy to help code that does not really work
I want to see if the project was published last month or not.
If yes then get a positive result.
//$project_time="2012-08-01 13:43:49";
$project_time="2012-10-02 14:05:09";
$end=mktime(0,0,0,date("m",strtotime($project_time))+1,date("d",strtotime($project_time)),d ate("y",strtotime($project_time)));
$end=date("d.m.y",$end);
$today=mktime(0,0,0,date("m"),date("d"),date("y"));
$today=date("d.m.y",$today);
echo 'Project date '.$date.'<br />';
echo 'End date '.$end.'<br />';
echo 'Today '.$today.'<br />';
if($today<$end){
echo " open<br />";
}
else{
echo " finish<br />";
}
PROJECT_TIME first gives a good result and the other not.
$ end create date based on $ PROJECT_TIME plus one month.
Variable data TOTDAY get today's date.
And the comparison I want to get an answer whether past month from PROJECTTIME
If someone has understood and can help I would be happy.
strtotime is the function you want to use. Simply use the following syntax:
$end = date('d.m.y', strtotime('+1 month', strtotime($project_time));
EDIT
What people are saying about comparing strings is correct. Don't compare strings, compare the timestamps.
Try rewriting it to something like:
$project_time = "2012-10-02 14:05:09";
$project_endtimestamp = strtotime('+1 month', strtotime($project_time));
echo 'Project date ' . $date . '<br />';
echo 'End date ' . date('d.m.y', $project_endtimestamp) . '<br />';
echo 'Today ' . date('d.m.y') . '<br />';
if (time() < $project_endtimestamp) {
echo " open<br />";
} else {
echo " finish<br />";
}
EDIT: Didn't completely understood the question. Added +1 month in strtotime call as per #Simon Germain answer.
Basic problem: You're comparing two "d.m.y" strings.
This will always fail, because PHP sees them as plain text, not as dates. Therefore, asking which one is bigger will generally give the wrong answer.
Also: Get rid of all that crazyness with the old-style date handling functions. PHP has much better ways to do that sort of thing these days.
$project_time="2012-10-02 14:05:09";
$projDate = DateTime::createFromFormat('Y-m-d H:i:s', $project_time);
$dateNow = new DateTime();
if($projDate < $dateNow) {
... do something here...
}
I'm writing a php script that iterates over the Monday of each week.
However the script seemed to get out of sync after 22nd of October.
<?php
$october_8th = strtotime("2012-10-08");
$one_week = 7 * 24 * 60 * 60;
$october_15th = $october_8th + $one_week;
$october_22nd = $october_15th + $one_week;
$october_29th = $october_22nd + $one_week;
$november_5th = $october_29th + $one_week;
echo date("Y-m-d -> l", $october_8th) . '<br />';
echo date("Y-m-d -> l", $october_15th) . '<br />';
echo date("Y-m-d -> l", $october_22nd) . '<br />';
echo date("Y-m-d -> l", $october_29th) . '<br />';
echo date("Y-m-d -> l", $november_5th) . '<br />';
This would output:
2012-10-08 -> Monday
2012-10-15 -> Monday
2012-10-22 -> Monday
2012-10-28 -> Sunday
2012-11-04 -> Sunday
I would expect it to say the 29th of October but it gets stuck at the 28th.
How should I get around this problem?
A preferred choice would be to use PHP's date-related classes to get the dates.
These classes importantly handle the daylight-savings boundaries for you, in a way that manually adding a given number of seconds to a Unix timestamp (the number from strtotime() that you used) cannot.
The following example takes your start dates and loops four times, each time adding a week to the date.
$start_date = new DateTime('2012-10-08');
$interval = new DateInterval('P1W');
$recurrences = 4;
foreach (new DatePeriod($start_date, $interval, $recurrences) as $date) {
echo $date->format('Y-m-d -> l') . '<br/>';
}
PHP Manual links:
The DatePeriod class
The DateInterval class
The DateTime class
While writing this question I discovered that day light saving time ends at the 28th of October.
Because the date at initialization doesn't contain a specific time automatically midnight is assigned. This however yields a problem when summertime ends. Suddenly the time isn't midnight anymore but one hour before that AND thus a day earlier then you would expect.
An easy fix would be to initialize the time to be midday instead of midnight:
$october_8th = strtotime("2012-10-08 12:00");
Perhaps there might be more elegant solution (you're welcome to leave one), but this will do for this purpose.
I need to add days to given string date and display calculated date in string
This is what I have tried, but I could not make it work.
$date = date_create('1-Feb-2012');
$newDate = date_modify($date, '+2 day');
echo 'Your date is' . $newDate . '.';
This gives an error
Object of class DateTime could not be converted to string
You need to tell the DateTime object how to format its output using DateTime::format. So, for example:
$date = new DateTime('1-Feb-2012');
$date->modify('+2 day');
echo 'Your date is' . $newDate->format('Y-m-d H:i:s') . '.';
Also note that modify directly modifies the DateTime - it doesn't just return a new one, as the documentation might lead you to believe - so I've removed the second variable. I've taken the liberty of changing the objects to the object-oriented form as well, which you should be using :)
Here's a working demo.
Use DateTime::format function.
usage:
echo 'Your date is' . $newdate->format('Y-m-d H:i:s') . '.';
or
echo 'Your date is' . date_format($newdate, 'Y-m-d H:i:s') . '.';