4

i have a date '07/23/2009' and a time '18:11' and i want to get a timestamp out of it : here is my example:

date_default_timezone_set('UTC');

$d = str_replace('/', ', ', '07/23/2009');
$t = str_replace(':', ', ', '18:11');
$date = $t.', 0, '.$d;
echo $date;
echo '<br>';
echo $x = mktime("$date");

the issue is that $x gives me the current timestamp.

any ideas?

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337

5 Answers5

6

it gives error because mktime function require all values of numbers only and this function gives only date . if you try like

$h = 18;
$i = 11;
$s = 00;
$m = 07;
$d =23;
$y = 2009;
echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y));

then it will work.

so your complete code will be

date_default_timezone_set('UTC');

$d = str_replace('/', ',', '07/23/2009');
$t = str_replace(':', ',', '18:11');
$date = $t.',0,'.$d;
$fulldate = explode(',',$date);
echo '<br>';
$h = $fulldate[0];
$i = $fulldate[1];
$s = $fulldate[2];
$m = $fulldate[3];
$d =$fulldate[4];
$y = $fulldate[5];

echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y)) . "<br>";

//if you want timestamp then use

echo strtotime("07/23/2009 18:11");

Thanks

Rajat Modi
  • 1,343
  • 14
  • 38
3

Use the DateTime class:-

$dateStr = '07/23/2009';
$timeStr = '18:11';
list($hours, $minutes) = explode(':', $timeStr);

$dateTime = \DateTime::createFromFormat('m/d/Y', $dateStr)->setTime($hours, $minutes);
$timeStamp = $dateTime->getTimestamp();

or:-

$dateStr = '07/23/2009 18:11';
$timestamp = \DateTime::createFromFormat('m/d/Y H:i', $dateStr)->getTimestamp();
vascowhite
  • 18,120
  • 9
  • 61
  • 77
2

Try using strtotime

$x = strtotime($date." ".$time);

for your case your code should be

date_default_timezone_set('UTC');
$x = strtotime("07/23/2009 18:11");
echo $x;
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • This may cause an issue with the date of `07/06/2009 18:11` - is that *7th of July* or the *6th of June*?. The reason is not everyone (in fact only a few countries) use MM/DD/YYYY format for dates, most use DD/MM/YYYY. Always best to store your dates in YYYY-MM-DD format. – Tigger Jul 11 '13 at 06:06
  • 1
    The way strtotime interprets it (at least in my locale) is if you are using `/` as the delimiter it is m/d/y if you are using `.` it is d.m.y and if you are using `-` it is y-m-d. But I agree Y-m-d is preferred (iso 8601) – Orangepill Jul 11 '13 at 06:18
1
<?php 

//current time zone to UTC
$date = new DateTime($datetime, new DateTimeZone(date_default_timezone_get()));

//set the time zone as UTC
$date->setTimezone(new DateTimeZone('UTC'));

//convert local time to UTC time
$date=$date->format("Y-M-D");

echo $date;

//--------------------------------------------------------------------

//UTC to some other time zone format

$date = new DateTime($datetime, new DateTimeZone("UTC"));

//set the time zone as UTC
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

//convert local time to UTC time
$date=$date->format("Y-M-D");

echo $date;
Sundar
  • 4,580
  • 6
  • 35
  • 61
0

IMPORTANT:

In addition to above answers ,there is an important thing that one must follow.Always use the With() function(see below) i.e

Always use :

$newTimezone = new DateTime($day);
$newTimezone->setTimezone(new DateTimeZone($timezone)); 

Do not use:

$newTimezone = new DateTime($day, new DateTimeZone($timezone));

REASON:(different outputs, check below)

function with($day,$timezone){
    $newTimezone = new DateTime($day);
    $newTimezone->setTimezone(new DateTimeZone($timezone)); 
    $timestamp = $newTimezone->format('U');
    return $timestamp;

}    
function without($day,$timezone){
    $newTimezone = new DateTime($day, new DateTimeZone($timezone));
    $timestamp = $newTimezone->format('U');
    return $timestamp;
}   
     
$tomorrow = date('Y-m-d h:i:s A', strtotime('-1 seconds ' ,strtotime('tomorrow midnight')));
$yesterday = date('Y-m-d h:i:s A', strtotime('+24 hours 1 seconds ' , strtotime('yesterday midnight')));
$timezone = 'UTC';
        


echo 'With Yesterday: '.with($yesterday,$timezone).'<br>';
    $now = new DateTime('@'.with($yesterday,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 

echo 'Without Yesterday: '.without($yesterday,$timezone).'<br>';
    $now = new DateTime('@'.without($yesterday,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 


echo 'With Tomorrow: '.with($tomorrow,$timezone).'<br>';
    $now = new DateTime('@'.with($tomorrow,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 


echo 'Without Tomorrow: '.without($tomorrow,$timezone).'<br>';
    $now = new DateTime('@'.without($tomorrow,$timezone));
    $now->setTimezone(new DateTimeZone(date_default_timezone_get()));
    echo 'With Yesterday Readable: '.$now->format('m/d/Y <b>h:i:s</b> A').' -------- '.date('m/d/Y <b>h:i:s</b> A').'<br><br>'; 

OUTPUTS:

With Yesterday: 1537642801

With Yesterday Readable: 09/23/2018 12:00:01 AM -------- 09/23/2018 10:05:55 PM

Without Yesterday: 1537660801

With Yesterday Readable: 09/23/2018 05:00:01 AM -------- 09/23/2018 10:05:55 PM

With Tomorrow: 1537729199

With Yesterday Readable: 09/23/2018 11:59:59 PM -------- 09/23/2018 10:05:55 PM

Without Tomorrow: 1537747199

With Yesterday Readable: 09/24/2018 04:59:59 AM -------- 09/23/2018 10:05:55 PM

Community
  • 1
  • 1
MR_AMDEV
  • 1,712
  • 2
  • 21
  • 38