wrong conversion in strtotime & in date() for 1345810203 - php

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

Related

Missing minutes in strtotime() / timestamp

Could somebody please take a look into my code:
$godzinaRozpoczecia = "10:35";
$dateStampGodzinaRozpoczecia = strtotime($godzinaRozpoczecia);
echo date("H:m", $dateStampGodzinaRozpoczecia);
RESULT: 10:08
var_dump($godzinaRozpoczecia);
RESULT: string(5) "10:35"
What is wrong? Why do I have minutes missing? In var_dump everything seems to be fine. The same issue occurs when I retrieve the time from the database.
The date() function specifies the following format for "H:m":
H: 24-hour format of an hour with leading zeros
m: Numeric representation of a month, with leading zeros
So you're actually showing the month here, you're looking for i:
i: Minutes with leading zeros
You may have been confused with the format used for the strtotime(), DateTime and date_create() formats. They are different from the date() formats (for some illogical reason).
Try this
date("H:i", $dateStampGodzinaRozpoczecia);
//m is for month.current month is August that is only it gives you 08 as aswer

Date('now') in PHP

After a long time I needed to use date function of PHP. I wrote something like:
echo date('now');
and I got the output below:
1220123
What does that mean ?
From the PHP manual :
n Numeric representation of a month, without leading zeros
o ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that
year is used instead. (added in PHP 5.1.0)
w Numeric representation of the day of the week
So, date("now") displays 12 (n), 2012 (o) and 3 (w).
You're probably looking for :
date("Y-m-d") for a date
date("Y-m-d H:i:s") for a datetime
"now" is not a valid parameter for for this expectation, infact it should be strtotime function here, not date.
Date considers your now as
n
Numeric representation of a month, without leading zeros
o
ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
w
Numeric representation of the day of the week
you need to give a valid format to date function (not recognize the 'now' string as meaning of now )
$date = date("Y-m-d H:i:s");
or you can use the DateTime class
$date = new DateTime();
Seems you consider "now" as a word to get the current date and time, however it would compile on each character. Here is the explanation how it'll compile.
n = Month in number
o = It considers as a year in ISO-8601.
w = Week in number
So that's why it's returning you the date, year and number of week in a month.
Hope I can explain you bit easily.
"now" is not a valid parameter for date()
Correct syntax to print current date in
yyyy-mm-dd hours minutes seconds
format is as given below
echo date('Y-m-d h:i:s');
also see PHP manual for details of date() function
http://php.net/manual/en/function.date.php

strtotime returning wrong time

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.

Localize output of strtotime string

I read through manuals concerning strtotime and strftime as well as related functions, but I cannot seem to make any progress in trying to solve my problem.
Basically, I'm interested in ways to print output of $deldate5 in local language (Dutch in this case).
$deldate5 = date("d.m.Y., l", strtotime("today + 5 day", time()));
I would most likely need to ditch strtotime string and replace it with something else in order to facilitate "today + 5 day" parameter.
Can anyone help? Thank you in advance.
Let's pick this apart:
$deldate5 = date("d.m.Y., l", strtotime("today + 5 day", time()));
This is doing two things:
strtotime: Create a UNIX timestamp (number of seconds since the epoch).
date: Format the output.
Your problem is related to the output (2.), not creating the timestamp (1.). So let's put this apart:
$timestamp = strtotime("today + 5 day", time());
$formatted = date("d.m.Y., l", $timestamp);
The only thing required now is to deal with the following line of code:
$formatted = date("d.m.Y., l", $timestamp);
The formatting parameters for the dateDocs function are:
d - Day of the month, 2 digits with leading zeros
m - Numeric representation of a month, with leading zeros
Y - A full numeric representation of a year, 4 digits
l - A full textual representation of the day of the week
As l (lower case L) requires a locale in your output, let's see which formatting parameter strftimeDocs has to offer that is similar:
%A - A full textual representation of the day.
So it's just a small step to change from date to strftime:
$formatted = strftime("%d.%m.%Y., %A", $timestamp);
Hope this helps.
Try setting the date default timezone to your local timezone:
http://www.php.net/manual/en/function.date-default-timezone-set.php

Wrong output from date()

I am getting the date from my MySQL database and that date is correct, but when I use the date() function the minute part is stuck at 6 minutes.
MySQL is returning 2010-06-15 09:59:18
Then in PHP I use this code ($row[4] is from my while loop):
date('M d,Y H:m A',strtotime($row[4]))
When I echo this out I get: Jun 15,2010 9:06 AM
I also have tried converting it to a unix timestamp in my SQL query, but it does the same thing. I noticed that the hours work and the seconds work but the minutes are stuck at 6. Does anyone have any ideas on what is going on?
'm' is a representation of month, not minutes using the PHP date() function. So, you are getting the '06', meaning June, not 06 minutes past 9.
It only looks like minutes because of how you formatted your date string but what you are getting there is a numerical representation of the month.
http://us2.php.net/manual/en/function.date.php
m - Numeric representation of a month, with leading zeros
I think the minute arg should be i (lowercase eye), not m.
Use this instead (i instead of m):
date('M d,Y H:i A',strtotime($row[4]))
The capital m is the month, and now it's June ;)
The minute is "i"

Categories