Skip to content

Commit ffbe501

Browse files
author
Derick Rethans
committed
- MFH: Added date_timestamp_get() / DateTime::getTimestamp() to retrieve the
Unix timestamp belonging to a date object.
1 parent 7932ccb commit ffbe501

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ PHP NEWS
1818
timezone_identifiers_list() / DateTimezone::listIdentifiers().
1919
* date_timestamp_set() / DateTime::setTimestamp() to set a Unix timestamp
2020
without invoking the date parser. (Scott)
21+
* date_timestamp_get() / DateTime::getTimestamp() to retrieve the Unix
22+
timestamp belonging to a date object.
2123

2224
- Added ability to store associative infor with objects in SplObjectStorage.
2325
(Marcus)

ext/date/php_date.c

+28
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ const zend_function_entry date_functions[] = {
182182
PHP_FE(date_date_set, NULL)
183183
PHP_FE(date_isodate_set, NULL)
184184
PHP_FE(date_timestamp_set, NULL)
185+
PHP_FE(date_timestamp_get, NULL)
185186

186187
PHP_FE(timezone_open, NULL)
187188
PHP_FE(timezone_name_get, NULL)
@@ -215,6 +216,7 @@ const zend_function_entry date_funcs_date[] = {
215216
PHP_ME_MAPPING(setDate, date_date_set, NULL, 0)
216217
PHP_ME_MAPPING(setISODate, date_isodate_set, NULL, 0)
217218
PHP_ME_MAPPING(setTimestamp, date_timestamp_set, NULL, 0)
219+
PHP_ME_MAPPING(getTimestamp, date_timestamp_get, NULL, 0)
218220
{NULL, NULL, NULL}
219221
};
220222

@@ -2267,6 +2269,32 @@ PHP_FUNCTION(date_timestamp_set)
22672269
}
22682270
/* }}} */
22692271

2272+
/* {{{ proto long date_timestamp_get(DateTime object)
2273+
Gets the Unix timestamp.
2274+
*/
2275+
PHP_FUNCTION(date_timestamp_get)
2276+
{
2277+
zval *object;
2278+
php_date_obj *dateobj;
2279+
long timestamp;
2280+
int error;
2281+
2282+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, date_ce_date) == FAILURE) {
2283+
RETURN_FALSE;
2284+
}
2285+
dateobj = (php_date_obj *) zend_object_store_get_object(object TSRMLS_CC);
2286+
DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
2287+
timelib_update_ts(dateobj->time, NULL);
2288+
2289+
timestamp = timelib_date_to_int(dateobj->time, &error);
2290+
if (error) {
2291+
RETURN_FALSE;
2292+
} else {
2293+
RETVAL_LONG(timestamp);
2294+
}
2295+
}
2296+
/* }}} */
2297+
22702298
static int timezone_initialize(timelib_tzinfo **tzi, /*const*/ char *tz TSRMLS_DC)
22712299
{
22722300
char *tzid;

ext/date/php_date.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ PHP_FUNCTION(date_time_set);
6363
PHP_FUNCTION(date_date_set);
6464
PHP_FUNCTION(date_isodate_set);
6565
PHP_FUNCTION(date_timestamp_set);
66+
PHP_FUNCTION(date_timestamp_get);
6667

6768
PHP_METHOD(DateTimeZone, __construct);
6869
PHP_FUNCTION(timezone_open);

0 commit comments

Comments
 (0)