2014-03-06T00:50:49.000Z
How can I convert this to a timestamp in PHP?
It is given as a string:
$time = "2014-03-06T00:50:49.000Z";
A slightly more OO way than the other answer...
<?php
$dt = new DateTime("2014-03-06T00:50:49.000Z");
echo $dt->getTimestamp();
I recommend this way because the DateTime class also has time manipulation and formatting methods. This is a class in 5.2.0+ , but there are implementations to mimic the DateTime class for earlier PHP versions.
strtotime() can handle that date format:
echo strtotime("2014-03-06T00:50:49.000Z");
// 1394067049
See it in action
Related
I have my date in the following format:
01-02-2019
and I need to get it into the following format:
2019-02-01T00:00:00.000
Is it strtotime I need? I find that a bit confusing to use.
strtotime("01-02-2019");
Thank you for your help.
As #Vidal said, it's look like ISO 8601 but it's a bit different on your example.
If this is the exact result you need, here's how:
echo \DateTime::createFromFormat('d-m-Y','01-02-2019')->format('Y-m-d\TH:i:s.v');
will display : 2019-02-01T17:38:33.000
More about the format parameters: http://php.net/manual/en/function.date.php
Edit: I would recommend Vidal answer if this exact format is not mandatory since it's respecting an ISO norme.
I think the format you want is ISO 8601.
Here the php code.
<?php
$unixTimestamp = date("c", strtotime('01-02-2019'));
?>
As Vidal said in their answer, I think the ISO 8601 standard is what you're after.
I personally prefer the OOP approach, and would recommend always using PHP's DateTime() class over strtotime(), but either will do what you're looking for.
To do the same formatting with DateTime, simply instantiate the DateTime object, and format it as you would with strtotime() like so:
// Build a date object
$myDate = new DateTime('01-02-2019');
// Format and display in the ISO 8601 standard using
// the 'c' format option.
echo $date->format('c');
The magic here is in the 'c' format string, which represents the ISO 8601 constant available to the DateTime class.
More info on DateTimeInterface constants
You can do this way if you want to convert it to another format
<?php
// Create a new DateTime object
echo date('Y-m-d\TH:i:s.v',strtotime('01-02-2019'));
?>
OR
<?php
// Create a new DateTime object
$date = DateTime::createFromFormat('d-m-Y', '01-02-2019');
echo $date->format('Y-m-d\TH:i:s.v');
?>
DEMO1: https://3v4l.org/IlCur
DEMO2: https://3v4l.org/6CMKT
I'm wondered why PHP DateTime class accepts a strange value to represent the timestamp?
The stranger value is #{$timestamp}:
Example:
$timestamp = time();
$date = new DateTime("#{$timestamp}");
PHP's Manual does not show any information related to #{$timestamp}!!
Please note that it's not possible to do something like this:
$date = new DateTime(time());
I know the easiest way to set a timestamp for DateTime class is: $date->setTimeStamp(time()); but I'm asking about #{$timestamp}
Does anyone know what is the magic behind #{$timestamp}?
Use of #timestamp is documented under Date/Time>Supported Date and Time Formats>Compound Formats>Localized Notations
Description Format Examples
Unix Timestamp "#" "-"? [0-9]+ "#1215282385"
How to convert this (in ISO8601 format): 2014-03-13T09:05:50.240Z
To this (in MySQL DATE format): 2014-03-13
in php?
try this
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
The complete date function documentation can be found here: http://php.net/manual/en/function.date.php
The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.
Hope I could help :)
P.s.:
Just in case strtotime will return 0 try using this:
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime(substr($date,0,10)));
Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):
$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d'); // MySQL datetime format
There is no reason to use the inefficient time functions. The most efficient way is to simply extract the first 10 characters:
substr($date,0,10)
People, that are really coding for year ≥10000, can use:
substr($date,0,strpos($date,"T"))
Simply convert datetime description into a Unix timestamp using with strtotime and then five format using Date Formats
Try it will surely work for you.
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
For those using Carbon (php library), the parse() works quite well:
Carbon::parse($date)
https://carbon.nesbot.com/docs/
Today I have published an interitty/utils package that deals with, among other things, the ISO-8601 format and perhaps all permutations of this standard.
I hope it will help you too.
$dateTimeFactory = new Interitty\Utils\DateTimeFactory();
$dateTime = $dateTimeFactory->createFromIso8601('1989-12-17T12:00:00Z');
I was wondering if anyone could help me out with this.
I am running PHP Version 5.2.16.
Up until now I have used a substring.
$Date = substr($Date,0,10);
$Date = mysql_real_escape_string($Date);
I am scraping this string from an REST Api, so I have no control over its format, and I am not sure if the 'T' in the middle of the string could cause a problem.
Now this does the job simply enough but I was looking to use something more elegant like
$Date = Date::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
but this just returns the error:
Fatal error: Class 'Date' not found in...
Im quite new to php but I am assuming that I require the Date class (Common sense) but how would I implement this class into my script?
Any help or suggestions would be greatly appreciated.
Thanks :)
You could do:
<?php
$Date = strtotime("2011-10-31T16:22:00");
$converted = date("Y-m-d", $Date);
echo ($converted);
?>
Look here http://codepad.org/TNIwXDRp
The class is called DateTime
$Date = DateTime::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
http://php.net/book.datetime
However, because the format is standardized (ISO 8601) it should not surprisingly change, so it should be safe to just split it into date and time yourself
list ($date, $time) = explode('T', $string);
The class is not called Date but DateTime and the createFromFormat() method requires PHP/5.3 or greater. Whatever, the format you have is correctly recognised by the constructor so all you need is:
<?php
$date = new DateTime('2011-10-31T16:22:00');
echo $date->format('Y-m-d');
You can either use substr to extract, or simply use strtotime. I have used both and they are both useful solutions. strtotime would be better if you also would need to extract the timestamp.
I want to input a timestamp in below format to the database.
yyyy-mm-dd hh:mm:ss
How can I get in above format?
When I use
$date = new Zend_Date();
it returns month dd, yyyy hh:mm:ss PM
I also use a JavaScript calender to insert a selected date and it returns in dd-mm-yyyy format
Now, I want to convert these both format into yyyy-mm-dd hh:mm:ss so can be inserted in database. Because date format not matching the database field format the date is not inserted and only filled with *00-00-00 00:00:00*
Thanks for answer
Not sure if this will help you, but try using:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
Technically, #stefgosselin gave the correct answer for Zend_Date, but Zend_Date is completely overkill for just getting the current time in a common format. Zend_Date is incredibly slow and cumbersome to use compared to PHP's native date related extensions. If you don't need translation or localisation in your Zend_Date output (and you apparently dont), stay away from it.
Use PHP's native date function for that, e.g.
echo date('Y-m-d H:i:s');
or DateTime procedural API
echo date_format(date_create(), 'Y-m-d H:i:s');
or DateTime Object API
$dateTime = new DateTime;
echo $dateTime->format('Y-m-d H:i:s');
Don't do the common mistake of using each and every component Zend Frameworks offers just because it offers it. There is absolutely no need to do that and in fact, if you can use a native PHP extension to achieve the same result with less or comparable effort, you are better off with the native solution.
Also, if you are going to save a date in your database, did you use any of the DateTime related columns in your database? Assuming you are using MySql, you could use a Timestamp column or an ISO8601 Date column.
This is how i did it:
abstract class App_Model_ModelAbstract extends Zend_Db_Table_Abstract
{
const DATE_FORMAT = 'yyyy-MM-dd';
public static function formatDate($date, $format = App_Model_ModelAbstract::DATE_FORMAT)
{
if (!$date instanceof Zend_Date && Zend_Date::isDate($date)) {
$date = new Zend_Date($date);
}
if ($date instanceof Zend_Date) {
return $date->get($format);
}
return $date;
}
}
this way you don't need to be concerned with whether or not its actually an instance of zend date, you can pass in a string or anything else that is a date.
a simple way to use Zend Date is to make specific function in its business objects that allows to parameter this function the date format. You can find a good example to this address http://www.pylejeune.fr/framework/utiliser-les-date-avec-zend_date/
this is i did it :
Zend_Date::now->toString('dd-MM-yyyy HH:mm:ss')
output from this format is "24-03-2012 13:02:01"
and you can modified your date format
I've always use $date->__toString('YYYY-MM-dd HH-mm-ss'); method in the past but today didn't work. I was getting the default output of 'Nov 1, 2013 12:19:23 PM'
So today I used $date->get('YYYY-MM-dd HH-mm-ss'); as mentioned above. Seems to have solved my problem.
You can find more information on this on output formats here: http://framework.zend.com/manual/1.12/en/zend.date.constants.html