How to change PHP date - php

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.

Related

Convert a datetime to another format using php

I am trying to convert the following datetime string into another format of date & time.
Thu Dec 15 10:05:14 +0000 2016
To
15 Dec
My code is
created_at = 'Thu Dec 15 10:05:14 +0000 2016';
$new_dt = date_format(strtotime($created_at),'jS F');
But it displays nothing.Any suggestion is highly appreciated.
date_format() is an alias for DateTime::format(), and it accepts DateTimeInterface instances (e.g. DateTime objects), not raw UNIX timestamps.
Also, relying on "magic" functions like strtotime() is bad in general.
What you need is the following:
$new_dt = DateTime::createFromFormat('D M d H:i:s O Y', $created_at)
->format('jS F');
Or if you really insist on using the procedural-style functions:
$new_dt = date_format(
date_create_from_format('D M d H:i:s O Y', $created_at),
'jS F'
);

php - using strtotime date goes to 1970s

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')));

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]);

gmmktime and mktime give me the same number

My PHP timezone is 'Asia/Tehran' so I would not expect gmmktime give me the same result as mktime.
php info:
Default timezone Asia/Tehran
However when I type in my php console:
echo date('jS F Y h:i:s A (T)', mktime());
3rd September 2013 11:23:10 AM (IRDT)
echo date('jS F Y h:i:s A (T)', gmmktime());
3rd September 2013 11:23:16 AM (IRDT)
While I expect 3.5 or 4.5 hours time difference. Am I making a mistake somewhere?
you need to give Argumente inside of gmmktime(). Your mistake is, understand first mktime() vs gmmktime(). see this link.
And The workaround is simple, use gmdate() function to display dates created with gmmktime().
<?php
$inputDate = gmmktime(0,0,0,2,7,2012);
echo gmdate("M d Y H:i:s O", $inputDate);
// Feb 07 2012 00:00:00 +0000
?>

Converting between timezones in PHP

I am converting this time and date:
Thu, 31 Mar 2011 02:05:59 GMT
To the following time and date format:
Monday March 28 2011 4:48:02 PM
I am using the following PHP code to accomplish this, but I want to convert all time zones to PST/PDT. I looked at the PHP manual and saw this date_default_timezone_set() but I am not sure how to implement that into the code I have below.
$date = $messages[0]->CreationTime;
echo date('l F j Y g:i:s A I', strtotime($date))
I would not use date_default_timezone_set for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)
Instead, I would use something like:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
$date = $messages[0]->CreationTime;
date_default_timezone_set('America/Los_Angeles');
echo date('l F j Y g:i:s A I', strtotime($date));
See this list for available timezones that get passed into the function

Categories