strtotime returning wrong time - php

echo date('Y-m-d h:m:s',strtotime('2008-11-03 16:00:29'))
Returns 2008-11-03 04:11:29
I've tried changing my default time zone and adding GMT,UTC,PTC behind the string and nothing will change the output. How do I get strtotime to match the input?

Use 'Y-m-d H:i:s' if you want 24-hour time, or add the am/pm to the end with 'Y-m-d h:i:s a'

You need i for minutes and H for 24-hour.
See date documentation.
m is month, which is why your time has 11 as minutes, the same as the month.

Related

PHP date format, AM and PM reversed

I am echoing out a date with the following code:
<?php echo date('h:i A', strtotime($catch[0]['catch_date'])); ?>
If I add it at 1:50PM it shows as: 1:50AM when echo'd out instead of showing PM.
I am stumped on this one. Any ideas?
Dates are being entered at the time of addition as:
'catch_date' => date('Y-m-d h:i:s')
You are using the wrong format.
From the manual:-
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
So you need to change your code to:-
'catch_date' => date('Y-m-d H:i:s')
You should set the default time zone before printing out the date. Example:
date_default_timezone_set('Pacific/Auckland');
And here is a full list of supported time zones:
https://www.php.net/manual/en/timezones.php

Date / Time showing odd value in PHP

I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime

wrong conversion in strtotime & in date() for 1345810203

I want to store start time & end time and calculate the total time difference between the two times.Some time i got negative times like -1 min -24 sec by this problem...
if i am echo this code
date('Y-m-d h:m:s',1345810203);
i got the answer as 2012-08-24 02:08:03 ,but i echo this code
strtotime('2012-08-24 02:08:03');
i got the answer as 1345766883 .
If the time will be 1345810167 or 1345810187,give correct time in strtotime & date().
What is the problem ?
You have wrong format.
m is Numeric representation of a month, with leading zeros
i is Minutes with leading zeros
Use following format:
'Y-m-d h:i:s'
Ref: http://php.net/manual/en/function.date.php

PHP Date from strtotime with current time

I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');

PHP mktime giving dates 12 off

I am writing a function to convert an internal date format into a timestamp value. However, when I print the date out in YYYY-MM-DD HH format the date is 12 hours off.
The code below gives me the wrong date and time. I am expecting 2011-03-25 13 but instead I am getting 2011-03-25 01.
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d h', $epoch);
When I use the following code I expect 2001-02-01 01 and get what I expected.
date_default_timezone_set("Europe/London");
$epoch = mktime(1,0,0,2,1,2011);
echo date('Y-m-d h', $epoch);
It seems that the 12 hour offset starts on March 25th in the 13th hour.
Any idea why this happens and how do I keep it from happening? Does this have to do with the different day light savings dates? The server timezone is set to "America/Los_Angeles".
It works, you're just using the wrong format code, take H (24 hour format) instead of h (12 hour format):
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d H', $epoch);
Read the PHP Manual, it explains each code in detail.

Categories