Convert This String to Timestamp PHP

Convert this string to timestamp PHP

In your code strtotime() is attempting to convert 13/10 as the tenth day of the 13th month, which returns an error.

If you want to parse a date string with a custom format, it's better to use DateTime::createFromFormat() instead:

$dtime = DateTime::createFromFormat("d/m G:i", "13/10 15:00");
$timestamp = $dtime->getTimestamp();

PHP Converting string to timestamp

You need to convert the timestamp string into a time first. If you want to ignore the timezone, you can do something like:

// Set the time
$time = "Mon Jul 13 11:32:00 EST 2015";
// Cut out the timezone details
$time = str_replace(" EST", "", $time);
// Convert the string time into a time and then into a timestamp
$timestamp = date("m/d/Y h:i:s A", strtotime($time));
// echo $timestamp = "07/13/2015 11:32:00 AM"

Or, you can use something like mktime() (http://php.net/mktime) but this requires you splitting up your time string into separate elements.

Convert String Timestamp to timestamp in PHP

It looks as a product of javascript's .getTime() function, returning number of milliseconds since 1970/01/01, hence 13 digits as opposed to timestamp being the number of seconds, so having 10 digits. Just divide it by 1000:

$time = date("Y-m-d", $timestamp/1000);
echo $time; // output: 2015-03-31

Converting string to Date and DateTime

Use strtotime() on your first date then date('Y-m-d') to convert it back:

$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time);

echo $newformat;
// 2003-10-16

Make note that there is a difference between using forward slash / and hyphen - in the strtotime() function. To quote from php.net:

Dates in the m/d/y or d-m-y formats
are disambiguated by looking at the
separator between the various
components: if the separator is a
slash (/), then the American m/d/y is
assumed; whereas if the separator is a
dash (-) or a dot (.), then the
European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

convert unique string to timestamp

Its prefer you clean your date string. You can use regex in this case which only allows the certain character to pass and remove all the rest special and hidden chars.

$dirty_date = 'Â Â 8:16:25 PM 8/8/2017Â Â';
echo $date = preg_replace("/[^0-9a-zA-Z \/:\-]/", "", $dirty_date);
echo "\n";
echo $xtime = strtotime($date);
echo "\n";
echo date("Y-m-d H:i:s",$xtime);

Converting String Date To Carbon Timestamp with Locale

You will want to get the time_zone string for example Europe/Paris and pass it in as a parameter, for example:

Carbon::createFromFormat('D, d M Y H:i:s e', $date, 'Europe/Paris')->toDateTimeString();

If you want GMT just use

Carbon::createFromFormat('D, d M Y H:i:s e', $date, 'UTC')->toDateTimeString();

It is one of the first things that comes up in the Carbon documentation...

Carbon::createFromFormat($format, $time, $tz);

http://carbon.nesbot.com/docs/#api-localization

Convert string timestamp into unix timestamp in PHP

Look into the PHP function strtotime:

strtotime — Parse about any English textual datetime description into a Unix timestamp

I'm not sure how well it handles / in dates, but replace them with - and it'll work.

Example

echo strtotime(str_replace("/","-","26/03/1982"));

outputs 385977600 which is the Unix date for Fri, 26 Mar 1982

You may need to set your date_default_timezone for this to work

Convert date string with timezone to timestamp

I fugured out !

uses the default time zone unless a time zone is specified in that
parameter
http://php.net/manual/en/function.strtotime.php

That's why the result is the same setting or not the timezone :

date_default_timezone_set('Europe/Paris');
print strtotime('2015-01-09T20:46:00+0200');
print "\n";
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');

print "\n\n";

date_default_timezone_set('UTC');
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');

output:

1420829160
1420832760
1420832760

1420832760
1420836360

Demo: https://eval.in/241781

Thanks for the help!

PHP Convert string into timestamp

This should work for you. You have received a urlencoded string, so you need to reverse this first.

FIDDLE

<?php
$timeStamp="Mon+Jul+13+10%3A52%3A00+EST+2015";
$timeStamp=urldecode($timeStamp);
echo strtotime($timeStamp);
?>


Related Topics



Leave a reply



Submit