php - using strtotime date goes to 1970s - php

I am trying to get next monday for a given date using strttotime but I am getting dates from Jan 1970 as ouput. Below is my code line which is written for the getting date of next monday, I got this code from here. Can anyone please help me understanding why this is happening. Thanks in advance.
Code:
$date_init = date('Y m d', strtotime('next monday', strtotime('2016 06 22')));
Expected Output:
2016 06 27
Actual Output:
1970 01 05

The string 2016 06 22 is not a valid date format according to the manual. Try to add hyphens:
$date_init = date('Y m d', strtotime('next monday', strtotime('2016-06-22')));
You can find all valid date formats here: http://php.net/manual/en/datetime.formats.date.php

Try this:
$date_init = date('Y m d', strtotime('next monday', strtotime('22-06-2016')));
'2016 06 22' needs to be formated to one of the formats mentioned here

2016 06 02 is not one of the accepted date formats as described in the PHP docs. For this reason, the inner call to strtotime returns FALSE as defined in the docs in case the given time string cannot be parsed.
Since this is not a valid input for the outer strtotime for the $now parameter, it takes the epoch, or January 1, 1970, as basis for it's calculations.
As a result, you end up with the next monday after January 1, 1970.
Removing the spaces from the initial date string will solve this:
$date_init = date('Y m d', strtotime('next monday', strtotime('20160622')));

Related

PHP How To Get Values From Dates

Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks
First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday
Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016
$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php
From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());

Format the formatted date for mysql

How can I format this date 01 August, 15 for mysql DATE field. So I need this 2015-08-01 format from 01 August, 15 this format in PHP. Tried following but not work
echo date('Y-m-d',strtotime('01 August, 15'));
It is because strtotime() does not understand what 01 August, 15 means. Try it:
var_dump(strtotime('01 August, 15')); // false
The 15 at the end is too ambiguous; it could be the day of the month or a short year.
The easiest way to make this work is probably to use DateTime::createFromFormat, like so:
$date = '01 August, 15';
$parsed = DateTime::createFromFormat('d F, y', $date);
echo $parsed->format('Y-m-d');
If you control the format of the date then you could also make it easier to parse. Formatting it like 01 August 2015 would work, for example.
First remove the , out of the date and then use the strtotime function.
So:
$date = "01 August, 15";
$date = str_replace(",", "", $date);
echo date("Y-m-d",strtotime($date));

PHP Date Conversion to Timestamp

I am trying to convert the following string (or strings of this time) to timestamps:
Closing date: 02 Apr 15
Closing date: 06 May 15
My code is as follows:
$start_date = explode("Closing date: ", $string);
$start_date = DateTime::createFromFormat('DD M yy', $start_date[1]);
But when I try echoing $start_date->getTimestamp() it tells me
Fatal error: Call to a member function getTimestamp() on a non-object
Any idea on what I may be doing wrong? DD M yy seems like the right date format to use.
It should be:
$start_date = explode("Closing date: ", $string);
$start_date = DateTime::createFromFormat('d M y', $start_date[1]);
The format you need to pass to DateTime::createFromFormat() to help it parse your data is d M y. I extracted the significance of the letters from the documentation:
d and j: Day of the month, 2 digits with or without leading zeros (01 to 31 or 1 to 31);
F and M: A textual representation of a month, such as January or Sept (January through December or Jan through Dec);
y: A two digit representation of a year (which is assumed to be in the range 1970-2069, inclusive); Examples: 99 or 03 (which will be interpreted as 1999 and 2003, respectively).
You need to change 'DD M yy' a to a right date format "d M y"
$start_date = explode("Closing date: ", 'Closing date: 02 Apr 15');
$start_date = DateTime::createFromFormat('d M y', $start_date[1]);
var_dump($start_date)
object(DateTime)[3430]
public 'date' => string '2015-04-02 15:27:28.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
DD M yy is actually not the right format. Taken from the DateTime::createFromFormat docs:
D and l - A textual representation of a day - Mon through Sun or Sunday
through Saturday
Try doing a var_dump of $start_date before calling getTimestamp, I bet is's actually false:
Returns a new DateTime instance or FALSE on failure.
The format you need to use is d M y.
Why not simply convert date to timestamps format using strtotime
$string="Closing date: 02 Apr 15";
$start_date = explode("Closing date: ", $string);
print strtotime($start_date[1]);

Convert date to two days from a given date

I'm trying to convert a given date to a date that's two days ahead of the given date. My code is as follows:
$date = date('D, M n', strtotime('+2 days', 'Mon, Dec 31, 2012'));
That code sort of gets it correct. It echoes "Wed, Jan 1". It gets the name of the day and the month correct. But, not the date. I have also tried another route.
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M n');
That didn't work either. Any ideas?
Thanks,
Lance
n is the format flag for month month. It's saying 1 because it's in January. Use j instead:
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M j'); //Wed, Jan 2
$newdate = date("D, M n",strtotime($oldDate. ' + 2 day'));

How to change PHP date

I am running Apache on CentOS. The date on CentOS is setup correct, but the date PHP is returning is 1970 I'm guessing it is something with PHP that I have to change.
How can I do this?
Are you passing in a second parameter to the date function that is NULL / empty? This would mean that the date you are producing - according to the format you specify in the first parameter - would be the unix epoch date (1 January 1970).
PHP counts time in seconds from 12:00am January 1, 1970 (as do most computer languages).
date('h:i:s d M Y', 0) => 12:00:00 01 Jan 1970;
date('h:i:s d M Y', null) => 12:00:00 01 Jan 1970;
date('h:i:s d M Y', false) => 12:00:00 01 Jan 1970;
date('h:i:s d M Y') => current date/time
See:
http://codepad.org/rm2QCaID
Make sure that your second parameter is filled in with a valid time.

Categories