PHP datetime to timestamp - php

I want to convert my date time which is in this format
2015-05-11T18:30:00+05:30
and I want to convert this to timestamp.
I have tried this code
$time = $event->start['dateTime']; // return 2015-05-11T18:30:00+05:30
echo date_timestamp_get($time);
But getting this error
Warning: date_timestamp_get() expects parameter 1 to be DateTime
How I can fix this?

First you need to create DateTime object. Like this:
$time = new DateTime($event->start['dateTime']);
After this since you now use OOP style you probably also want to use OOP style to get your timestamp and so that you don't mix procedural style with OOP style, e.g.
$time->getTimestamp();
See getTimestamp() for more information and the difference about the procedural style and the OOP style.

Related

PHP DateTime class accepts #{$timestamp} as a timestamp. why?

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 can I convert this to a timestamp?

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

PHP Zend date format

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

PHP Datetime not working properly

I am retrieving the datetime data from mysql the retrieved data from a single row is.
2011-04-11 19:31:30
I wanted to reformat the datetime in d-m-Y H:i:s for that I am using date_format() the below code works just fine.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
However, I don't want to go object oriented way just for reformatting because I will be using the code within the foreach loop and that means I would have to initialize the DateTime class again and again.
I tried doing this the Procedural way using the following code.
$date = $users['registerDate'];
echo date_format($date, 'Y-m-d H:i:s');
the above code does not work for me and gives back the following error.
Warning: date_format() expects parameter 1 to be DateTime, string given in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/users/users.php on line 21
What could be possibly wrong?
The given solution works perfectly fine for the procedural way.
echo date('m-d-Y',strtotime($users['registerDate']));
However I would like to know which will be the best feasible solution the above procedural way or the OOP way.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
Considering I will be using the code within a foreach loop and it may loop for over a hundred times.
You do not need "date_format()":
echo date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
//results: 11-04-2011 19:31:30
You're trying to use a function/object that is part of the DateTime class without creating a reference to the DateTime class.
For procedural formatting take a look at date()
What you are seeking for is this:
date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
Have a look at the php-manual. However, using the methods you yourself proposed is pretty fine, since the DateTime-object maps to some functions written in C.
Also PHP Datetime is working properly, since date_format is just an alias of Date::format, which does exactly require what you don’t want to pass in (a DateTime-object).
Honestly, we’re talking about PHP...

PHP DateTime for .NET developers

PHP's DateTime class has the two methods add() and sub() to add or subtract a time span from a DateTime object. This looks very familiar to me, in .NET I can do the same thing. But once I tried it, it did very strange things. I found out that in PHP, those methods will modify the object itself. This is not documented and the return value type indicates otherwise. Okay, so in some scenarios I need three lines of code instead of one, plus an additional local variable. But then PHP5's copy by reference model comes into play, too. Just copying the object isn't enough, you need to explicitly clone it to not accidently modify the original instance still. So here's the C# code:
DateTime today = DateTime.Today;
...
if (date < today.Add(TimeSpan.FromDays(7)) ...
Here's the PHP equivalent:
$today = new DateTime('today');
...
$then = clone $today;
$then->add(new DateInterval('P7D'));
if ($date < $then) ...
(I keep a copy of today to have the same time for all calculations during the method runtime. I've seen it often enough that seconds or larger time units change in that time...)
Is this it in PHP? I need to write a wrapper class for that if there's no better solution! Or I'll just stay with the good ol' time() function and just use DateTime for easier parsing of the ISO date I get from the database.
$today = time() % 86400;
...
if ($date < $today + 7 * 86400) ...
Time zones not regarded in these examples.
Update: I just found out that I can use the strtotime() function as well for parsing such dates. So what's the use for a DateTime class in PHP after all if it's so complicated to use?
Update^2: I noticed that my $today = ... thing above is garbage. Well, please just imagine some correct way of getting 'today' there instead...
It looks like you will definitely have to make a clone of the object. There does not seem to be a way to create a copy of the DateTime object any other way.
As far as I can see, you could save one line:
$today = new DateTime('today');
...
$then = clone $today;
if ($then->add(new DateInterval('P7D')) < $then) ...
I agree this isn't perfect. But do stick with DateTime nevertheless - write a helper class if need be. It is way superior to the old date functions: It doesn't have the year 2038 bug, and it makes dealing with time zones much, much easiert.

Categories