I tried
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
UPDATE
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
22 Jan 2011 12:00:00 am
You can call ->setTime(0, 0) to zero out the time portion:
$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
See the documentation for DateTime::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If you do the following function call, you'll get the result you expect:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
Today's start timestamp
$todayStartTS = strtotime(date('Y-m-d', time()) . ' 00:00:00');
You can do this by passing the current unix timestamp as the second parameter to the date function
echo date("Y-m-d H:i:s",time());
Remove this part g:i:s a from your code.
Now, if you want a nice date formatted according to your local, i recommand you to use strftime() function.
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have.
I suggest you take a peak at the php date documentation.
Is it using UTC, or something?
I have a PHP version that gives me an error whenever I do something date related without first using date_default_timezone_set. Maybe that'll help you.
Related
I am storing date time in database as $date=date("Y-m-d H:i:s"); Now on most places I use it in the same format as it is stored 2016-03-01 19:04:18, but on one place I would rather prefer output to be March 01 at 19:04 how can I change format of output from database from 2016-03-01 19:04:18 to March 01 at 19:04.
Working with dates in PHP
First of all we will need to retrieve the date from the database. You can do this however you please.
What we are going to do next is to pass your date to a PHP DateTime object. This will give us easy options to modify the format to our liking.
// $dbDate is the date you got from your database
$date = new DateTime($dbDate);
// If we want to make sure PHP uses the correct date format,
// We can also use createFromFormat
$date = DateTime::createFromFormat('Y-m-d H:i:s', $dbDate);
Now that we have this object we are going to output it in the format you want it to be.
echo $date->format('M d').' at '.$date->format('H:i');
// M - Textual representation of the month ex. January, February, March
// d - Day of month in numeric value ex 1, 14, 27
// H - 24-hours representation of the hour ex 17, 23, 08
// i - Minutes ex 23, 45, 58
For more parameters and information:
PHP.net - DateTime()
Format it like this
date('d-M-Y H:i');
instead of
date("Y-m-d H:i:s");
Play around with these d-M-Y H:i to get the desired result
parse the date variable to it to tell it which variable to format
date("Y-m-d H:i:s", $date);
also see documentation here on date function in PHP
http://php.net/manual/en/function.date.php
I am using a plugin to create wordpress posts from a twitter feed, and I am trying to edit it so the published time is the same as the tweeted time, rather than the time the cron was run.
Unfortunately, twitter's API returns an already formatted date string, instead of a timestamp, so I am having to parse it and then save it in a wordpress friendly format.
// Wed Jun 06 20:07:10 +0000 2012 (Twitter formatted date example)
// 2014-03-10 18:30:26 (Wordpress formatted date example)
$tweet_date = $tweet->created_at;
$tweet_date = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = date("Y-m-d h:i:s", $tweet_date);
Unfortunately, all I am getting from this the Unix Epoch (Jan 1st, 1970).
I know I must be missing a step, but I can't figure out where.
You had two issues:
1) You were using h for hours when you meant H for 24 hour periods
2) You need to use date_format() when using date_create_from_format() as that function returns a DateTime object which is not compatible with date()
$tweet_date = date_create_from_format("D M d H:i:s O Y", 'Wed Jun 06 20:07:10 +0000 2012');
echo date_format($tweet_date, 'Y-m-d H:i:s');
See it in action
The problem is because you're mixing and matching between PHP's old and new-style date handling.
date_create_from_format() is part of the newer API, and outputs a DateTime object, not the timestamp integer that the older date() function is expecting.
Ideally you should stick entirely with either the new or the old date functions. You can switch between them, but there usually isn't a need to.
For example, in your case, the DateTime object generated by date_create_from_format() has a perfectly usable format() method attached to it, which does exactly the same as the date() function, but on a DateTime object.
$tweet_date_object = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = $tweet_date_object->format("Y-m-d h:i:s");
I am using php to reformat a date and post it to mysql. Everything works great until I pass dates for next year. For example Mon, 14 Jan, 2013 will be translated into 2012-01-16. The format is correct just not the date, I have even tried changing the format I pass it, still no change. Here is what it gets Mon, 14 Jan, 2013 and here is the php that processes it:
$startdate = $_REQUEST['one'];
$start = date("Y-m-d", strtotime($startdate));
any clues as to why the hiccup happens only when we enter a new year, even past years?
Have a look here for the list of all valid formats for strtotime(). The one you're using is not present.
If you want to use date_create_from_format instead, here's how:
date_create_from_format("D, d M, Y", "Mon, 14 Jan, 2013")
the date function is displaying 07-12-2012 instead of 7-12-2012
I am using this date("Y-m-d H:i:s")
I want to display 7th December 2012 as 7-12-2012
what about displaying month as 1-1-2012 rather than 01-01-2012 for 1st Jan 2012
Use this instead:
date("Y-m-j H:i:s")
d shows the days with leading zeros, j just shows the days.
Edit: On second glance, the string you are using is totally different to the output you want:
date("j-m-Y");
Should get you what you want.
Use j instead of d.
echo date('j-m-Y');
Change to:
date("j-m-Y");
Read about variables here.
I have a timezone of the user(he chooses it from a list)
I have a time in UTC(not current time)
So I need something like GetTimeForRegion(time, timezone) for PHP. Is there such functions or libraries or services?
you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844);
$date->setTimezone(new DateTimeZone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04
date('r') or date('c') may help you.
echo date('r') prints Thu, 16 Feb 2011 16:01:07 +0200
echo date('c') prints 2011-02-16T16:01:07+02:00
You need to look at the Date/Time API in PHP. I strongly advise you to stay away of gmdate and older date functions in php.
In your case, you should ask the user for its Olson based time zone.
The code of Artefact2 will do the trick.
please write this instead :
$date = date("Y-m-d H:i:s" , time());
so Y it means year m means month d means day
H get hours from 0 - 24
h get hours from 0 to 12
i get minutes
s get seconds